/* GUI CONVERTED from rcrsupb.p (converter v1.76) Mon Aug  5 02:20:24 2002 */
/* rcrsupb.p - Release Management Customer Schedules                    */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Revision: 1.44 $                                                         */
/*V8:ConvertMode=Report                                                 */
/* REVISION: 7.0    LAST MODIFIED: 01/29/92           BY: WUG *F110*    */
/* REVISION: 7.0    LAST MODIFIED: 03/16/92           BY: WUG *F312*    */
/* REVISION: 7.0    LAST MODIFIED: 03/26/92           BY: WUG *F323*    */
/* REVISION: 7.0    LAST MODIFIED: 06/12/92           BY: WUG *F606*    */
/* REVISION: 7.3    LAST MODIFIED: 09/25/92           BY: WUG *G094*    */
/* REVISION: 7.0    LAST MODIFIED: 06/16/92           BY: WUG *G462*    */
/* REVISION: 7.3    LAST MODIFIED: 03/22/93           BY: WUG *G857*    */
/* REVISION: 7.3    LAST MODIFIED: 04/05/93           BY: WUG *G902*    */
/* REVISION: 7.3    LAST MODIFIED: 06/21/93           BY: WUG *GC53*    */
/* REVISION: 7.3    LAST MODIFIED: 08/13/93           BY: WUG *GE19*    */
/* REVISION: 7.3    LAST MODIFIED: 09/14/93           BY: WUG *GF21*    */
/* REVISION: 7.4    LAST MODIFIED: 09/29/93           BY: WUG *H142*    */
/* REVISION: 7.3    LAST MODIFIED: 02/07/94           BY: WUG *GI62*    */
/* REVISION: 7.3    LAST MODIFIED: 03/29/94           BY: WUG *GJ22*    */
/* REVISION: 7.4    LAST MODIFIED: 09/11/94           BY: rmh *GM22*    */
/* REVISION: 7.4    LAST MODIFIED: 11/01/94           BY: ame *GN84*    */
/* REVISION: 7.4    LAST MODIFIED: 01/24/95           BY: jxz *G0D1*    */
/* REVISION: 7.4    LAST MODIFIED: 03/23/95           BY: bcm *G0J3*    */
/* REVISION: 7.4    LAST MODIFIED: 03/29/95           BY: bcm *G0JS*    */
/* REVISION: 7.4    LAST MODIFIED: 10/22/95           BY: vrn *G0YL*    */
/* REVISION: 7.4    LAST MODIFIED: 03/05/96           BY: kjm *G1PP*    */
/* REVISION: 7.4    LAST MODIFIED: 06/14/96           BY: rxm *G1Y8*    */
/* REVISION: 8.6    LAST MODIFIED: 09/20/96           BY: TSI *K005*    */
/* REVISION: 8.6    LAST MODIFIED: 10/22/96 BY: *G2GR* Sanjay Patil     */
/* REVISION: 8.6    LAST MODIFIED: 11/22/96 BY: *K02B* Chris Theisen    */
/* REVISION: 8.6    LAST MODIFIED: 04/08/97 BY: *J1NL* Vinay Nayak-Sujir*/
/* REVISION: 8.6    LAST MODIFIED: 05/16/97 BY: *H0Z5* Aruna Patil      */
/* REVISION: 8.6    LAST MODIFIED: 06/09/97 BY: *G2N9* Seema Varma      */
/* REVISION: 8.6    LAST MODIFIED: 10/15/97 BY: *J232* Niranjan R.      */
/* REVISION: 8.6    LAST MODIFIED: 10/22/97 BY: *J234* Niranjan R.      */
/* REVISION: 8.6E   LAST MODIFIED: 02/23/98 BY: *L007* A. Rahane        */
/* REVISION: 8.6E   LAST MODIFIED: 04/15/98 BY: *J2GD* Samir Bavkar     */
/* REVISION: 8.6E   LAST MODIFIED: 05/20/98 BY: *K1Q4* Alfred Tan       */
/* REVISION: 8.6E   LAST MODIFIED: 07/29/98 BY: *J2V0* Surekha Joshi    */
/* REVISION: 8.6E   LAST MODIFIED: 08/27/98 BY: *J2XX* Seema Varma      */
/* REVISION: 8.6E   LAST MODIFIED: 10/12/98 BY: *J321* Surekha Joshi    */
/* REVISION: 9.0    LAST MODIFIED: 02/06/99 BY: *M06R* Doug Norton      */
/* REVISION: 9.0    LAST MODIFIED: 03/13/99 BY: *M0BD* Alfred Tan       */
/* REVISION: 9.1    LAST MODIFIED: 09/10/99 BY: *N01B* Mugdha Tambe     */
/* REVISION: 9.1    LAST MODIFIED: 08/19/99 BY: *K229* Surekha Joshi    */
/* REVISION: 9.1    LAST MODIFIED: 05/20/99 BY: *N004* Patrick Rowan    */
/* REVISION: 9.1    LAST MODIFIED: 03/01/00 BY: *N03S* Jeff Wootton     */
/* REVISION: 9.1    LAST MODIFIED: 03/24/00 BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1    LAST MODIFIED: 04/11/00 BY: *J3PK* Reetu Kapoor     */
/* REVISION: 9.1    LAST MODIFIED: 06/06/00 BY: *N0CX* Jean Miller      */
/* REVISION: 9.1    LAST MODIFIED: 07/11/00 BY: *N0FD* Inna Lyubarsky   */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00 BY: *N0KP* myb              */
/* REVISION: 9.1    LAST MODIFIED: 10/30/00 BY: *L15C* Kirti Desai      */
/* REVISION: 9.1    LAST MODIFIED: 11/09/00 BY: *N0RG* BalbeerS Rajput  */
/* Old ECO marker removed, but no ECO header exists *F0PN*              */
/* Revision: 1.40     BY: Kirti Desai    DATE: 06/13/01 ECO: *M1B2*     */
/* Revision: 1.41     BY: Anitha Gopal   DATE: 02/17/02 ECO: *N19G*     */
/* Revision: 1.42     BY: Mercy C.       DATE: 04/25/02 ECO: *N1H5*     */
/* Revision: 1.43     BY: Mercy C.       DATE: 05/13/02 ECO: *N1J6*     */
/* $Revision: 1.44 $    BY: Kirti Desai    DATE: 07/31/02 ECO: *N1Q3*        */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/


/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/*! REQUIRED SCHEDULE UPDATE SUBPROGRAM */

define input parameter sonbr like so_nbr.
define input parameter soline like sod_line.
define input parameter plan_rlse_id like sch_rlse_id.
define input parameter ship_rlse_id like sch_rlse_id.
define input parameter seq_rlse_id like sch_rlse_id no-undo.

define new shared variable ship_dlvy_code     as character.
define new shared variable req_dt             as date.
define new shared variable req_qty            as decimal.
define new shared variable resch_ct           as integer.
define new shared variable resch_dt           as date    extent 999 no-undo.
define new shared variable resch_qty          as decimal extent 999 no-undo.
define new shared variable working_hours      as decimal extent 999 no-undo.
define new shared variable interval_code      as character.
define new shared variable resch_stat         as character.
define new shared variable l_inc_wkend_shpcal like so_incl_iss
   no-undo.
define new shared variable shipto_calid as character.

/* SINCE VARIABLE HAD TO BE DEFINED AS SHARED IN rcordmlt.p */
define new shared variable active_rlse_id like sch_rlse_id no-undo.

define shared variable report_yn like mfc_logical
   label "Report" initial yes.
/* DEFAULT VALUE FOR UPDATE_YN IS CHANGED FROM YES TO NO */
/* SINCE IT IS RECOMMENDED APPROACH IN HELP DOCUMENTATION */
define shared variable update_yn like mfc_logical no-undo.
define shared variable warn_ct as integer no-undo.
define shared variable using_seq_schedules
   like mfc_logical no-undo.

define variable schtype like sch_type initial 3.
define variable plan_schedule as integer initial 1.
define variable ship_schedule as integer initial 2.
define variable req_schedule as integer initial 3.
define variable shipto as character format "x(38)" extent 6.
define variable overdue like schd_upd_qty.
define variable i as integer.
define variable ii as integer.
define variable iweeks as integer.
define variable amt_to_reduce like schd_upd_qty.
define variable display_qty like schd_upd_qty.
define variable overshipped_left like schd_upd_qty.
define variable shm_recid as recid.
define variable ship_date as date.
define variable interval as decimal.
define variable start_date as date.
define variable shipfrom_date as date.
define variable shipfrom_time as character.
define variable plan_sd_dates as logical no-undo.
define variable ship_sd_dates as logical no-undo.
define variable ship_plan_pcr as logical no-undo.
define variable week_ship_sched_total like schd_upd_qty.
define variable temp_cmtindx like cmt_indx.
define variable plan-cmtindx like cmt_indx no-undo.
define variable ship-cmtindx like cmt_indx no-undo.
define variable last-cmtseq  like cmt_seq no-undo.
define variable new_index like cmt_indx.
define variable prior_cum_req like sch_pcr_qty.
define variable ship_prior_cum_req like sch_pcr_qty.
define variable plan_prior_cum_req like sch_pcr_qty.
define variable adjust_qty like schd_cum_qty.
define variable plan_coverage_qty like schd_cum_qty.
define variable first_ship_date as date.
define variable last_ship_date as date.
define variable last_plan_week_date as date.
define variable plan_month as integer.
define variable prior_cum_req_date as date.
define variable ret_status as character.
define variable where_from as character
   format "x(15)" label "From".
define variable arrive_date as date.
define variable arrive_time as character.
define variable shipfrom_calid as character.
define variable temp_qty as decimal extent 6.
define variable temp_date as date.
define variable cal_msg as character.
define variable so_translt_days like sod_translt_days.
define variable dummy as character format "x(13)".
define variable this_is_last_ship_week like mfc_logical.
define variable new_ship_qty like schd_discr_qty.
define variable last_week_plan_cum_qty like schd_discr_qty.
define variable last_week_ship_cum_qty like schd_discr_qty.
define variable auth_nbrs as decimal no-undo.
define variable auth_qty as decimal extent 999 no-undo.
define variable auth_date as date extent 999 no-undo.
define variable new_qty as decimal no-undo.
define variable mod_qty as decimal no-undo.
define variable rqmdet_recid as recid no-undo.
define variable rqmvalue as character no-undo.
define variable rqmrelease as character no-undo.
define variable rqmdate as date no-undo.
define variable rqmcnt as integer no-undo.
define variable s as integer no-undo.
define variable n as integer no-undo.
define variable x as integer no-undo.
define variable msgtext as character no-undo.
define variable errlevel as integer no-undo.
define variable undo_status as logical no-undo.
define variable l_ship_cumulative as decimal.
define variable l_plan_cumulative as decimal.
define variable is_req_schd_maint like mfc_logical initial no no-undo.
define variable last_seq_week_date like schd_date no-undo.
define variable seq_cumulative like schd_cum_qty no-undo.
define variable seq_weekly_cum like schd_cum_qty no-undo.
define variable seq_end_of_week like mfc_logical no-undo.
define variable seq_next_open_date like schd_date no-undo.
define variable ship_interval like schd_interval no-undo.
define variable first_plan_date like schd_date no-undo.
define variable last_plan_date like schd_date no-undo.
define variable seq_sd_dates like sch_sd_dates no-undo.
define variable seq_prior_cum_req like sch_pcr_qty no-undo.
define variable first_plan_cumulative like schd_cum_qty no-undo.
define variable first_ship_cumulative like schd_cum_qty no-undo.
define variable last_seq_date like schd_date no-undo.
define variable first_ship_week_date like schd_date no-undo.
define variable last_week_seq_cum_qty like schd_discr_qty no-undo.
define variable this_week_seq_cum_qty like schd_discr_qty no-undo.
define variable seq_schedule as integer initial 4 no-undo.
define variable new_work_qty like schd_upd_qty no-undo.
define variable new_work_date like schd_date no-undo.
define variable plan_sd_pat like sch_sd_pat no-undo.
define variable ship_sd_pat like sch_sd_pat no-undo.
define variable temp_ship_prior_cum_req like sch_pcr_qty no-undo.
define variable temp_plan_prior_cum_req like sch_pcr_qty no-undo.
define variable l_msg like msg_mstr.msg_desc no-undo.

define buffer cmtdet for cmt_det.
define buffer ship_mstr for sch_mstr.
define buffer ship_det for schd_det.
define buffer prev_sch_mstr for sch_mstr.
define buffer next_schd_det for schd_det.
define buffer prevrqm for rqm_det.
define buffer absrdet for absr_det.

define new shared workfile work_seq_schd no-undo
   field work_seq_week_date like schd_date
   field work_seq_date like schd_date
   field work_seq_time like schd_time format "XX:XX"
   field work_seq_reference like schd_reference
   field work_seq_qty like schd_upd_qty
   field work_seq_cum_qty like schd_cum_qty
   field work_seq_rlse_id as character
   field work_seq_recid as recid
   field work_seq_cmtindx like sch_cmtindx.

define workfile work_ship_schd
   field work_ship_week_date like schd_date
   field work_ship_date like schd_date
   field work_ship_time like schd_time format "XX:XX"
   field work_ship_reference like schd_reference
   field work_ship_qty like schd_upd_qty
   field work_ship_cum_qty like schd_cum_qty
   field work_ship_rlse_id as character
   field work_ship_recid as recid
   field work_ship_cmtindx like sch_cmtindx.

/* ADDED THE FIELD work_plan_interval TO THE work_plan_schd WORKFILE */
/* TO DIFFERENTIATE BETWEEN THE WORKFILE RECORDS FROM MONTHLY        */
/* SCHEDULE AND WORKFILE RECORDS FROM DISCRETE OR WEEKLY SCHEDULES   */

define workfile work_plan_schd
   field work_plan_week_date like schd_date
   field work_plan_date like schd_date
   field work_plan_time like schd_time format "XX:XX"
   field work_plan_reference like schd_reference
   field work_plan_qty like schd_upd_qty
   field work_plan_cum_qty like schd_cum_qty
   field work_plan_rlse_id as character
   field work_plan_recid as recid
   field work_plan_interval  like schd_interval
   field work_plan_cmtindx like sch_cmtindx.

define workfile work_schd
   field work_type like sch_type
   field work_date like schd_date
   field work_time like schd_time format "XX:XX"
   field work_reference like schd_reference
   field work_qty like schd_upd_qty
   field work_cum_qty like schd_cum_qty
   field work_rlse_id as character
   field work_recid as recid
   field work_cmtindx like sch_cmtindx.

define workfile work_rqm
   field work_rqm_value as character.

/* SINCE TEMP-TABLE HAD TO BE DEFINED AS SHARED IN rcordmlt.p */
define new shared temp-table t_schd_det no-undo like schd_det.

FORM /*GUI*/ 
   work_plan_date
   work_plan_time
   work_plan_reference
   work_plan_qty
   space(5)
   adjust_qty      column-label "Adjustment Qty"
                   format "->,>>>,>>9.9<<<"
   new_work_date   column-label "New Date"
   new_work_qty    column-label "New Qty"
   with STREAM-IO /*GUI*/  frame planovlap down
   title color normal
   (getFrameTitle("PLAN_SCHD_ADJ_FOR_SHIP_SCHD_OVERLAP",68)) width 80.

/* SET EXTERNAL LABELS */
setFrameLabels(frame planovlap:handle).

FORM /*GUI*/ 
   with STREAM-IO /*GUI*/  frame shipsdp down
   title color normal
   (getFrameTitle("SHIP_SCHED_PER_SHIP_DEL_PATTERN",75)) width 80.

/* SET EXTERNAL LABELS */
setFrameLabels(frame shipsdp:handle).

FORM /*GUI*/ 
   with STREAM-IO /*GUI*/  frame plansdp down
   title color normal
   (getFrameTitle("PLAN_SCHED_PER_SHIP_DEL_PATTERN",75)) width 80.

/* SET EXTERNAL LABELS */
setFrameLabels(frame plansdp:handle).

FORM /*GUI*/ 
   with STREAM-IO /*GUI*/  frame consume_adjust down
   title color normal
   (getFrameTitle("CONSUME_LOGIC_ADJ_TO_SHIP_SCHED",63)) width 80.

/* SET EXTERNAL LABELS */
setFrameLabels(frame consume_adjust:handle).

FORM /*GUI*/ 
   with STREAM-IO /*GUI*/  frame seq_consume_adjust down
   title color normal
   (getFrameTitle("CONSUME_LOGIC_ADJ_TO_SEQ_SCHED",63))  width 80.

/* SET EXTERNAL LABELS */
setFrameLabels(frame seq_consume_adjust:handle).

FORM /*GUI*/ 
   sod_conrep  colon 25
   code_cmmt   colon 35    no-label
   with STREAM-IO /*GUI*/  frame net-logic side-labels
   title color normal
   (getFrameTitle("NETTING_OPTIONS",23))  width 80 attr-space.

/* SET EXTERNAL LABELS */
setFrameLabels(frame net-logic:handle).

FORM /*GUI*/ 
   sod_conrep  colon 25
   code_cmmt   colon 35    no-label
   ship_plan_pcr colon 25  label "Use Ship/Plan PCR"
   with STREAM-IO /*GUI*/  frame net-logic-pcr side-labels
   title color normal
   (getFrameTitle("NETTING_OPTIONS",23))  width 80 attr-space.

/* SET EXTERNAL LABELS */
setFrameLabels(frame net-logic-pcr:handle).

FORM /*GUI*/ 
   work_plan_date
   work_plan_time
   work_plan_reference
   work_plan_qty
   space(5)
   adjust_qty      column-label "Adjustment Qty"
                   format "->,>>>,>>9.9<<<"
   new_work_date   column-label "New Date"
   new_work_qty    column-label "New Qty"
   with STREAM-IO /*GUI*/  frame seq_plan_ovlap down
   title color normal
   (getFrameTitle("SEQ_SCHED_ADJ_FOR_PLAN_SCHED",67))  width 80.

/* SET EXTERNAL LABELS */
setFrameLabels(frame seq_plan_ovlap:handle).

FORM /*GUI*/ 
   work_ship_date
   work_ship_time
   work_ship_reference
   work_ship_qty
   space(5)
   adjust_qty      column-label "Adjustment Qty"
                   format "->,>>>,>>9.9<<<"
   new_work_date   column-label "New Date"
   new_work_qty    column-label "New Qty"
   with STREAM-IO /*GUI*/  frame seq_ship_ovlap down
   title color normal
   (getFrameTitle("SEQ_SCHED_ADJ_FOR_SHIP_SCHED",67))  width 80.

/* SET EXTERNAL LABELS */
setFrameLabels(frame seq_ship_ovlap:handle).

find sod_det where sod_nbr = sonbr and sod_line = soline exclusive-lock.
find so_mstr where so_nbr = sod_nbr no-lock.

assign
   l_inc_wkend_shpcal = so_incl_iss
   so_translt_days    = decimal(substring(so_conrep,2,6)).

run delete_work_schd_tables.

if can-find(shop_cal where shop_site = so_ship and shop_wkctr = ""
   and shop_mch = "")
then
   assign
      shipto_calid = so_ship
      cal_msg      = getTermLabel("USING_SHIP-TO_CALENDAR",40) + " "
                     + shipto_calid.
else
if can-find(shop_cal where shop_site = so_cust and shop_wkctr = ""
   and shop_mch = "")
then
   assign
      shipto_calid = so_cust
      cal_msg      = getTermLabel("USING_CUSTOMER_CALENDAR",40) + " "
                     + shipto_calid.
else do:
   shipto_calid = ?.
   {pxmsg.i &MSGNUM=4550 &ERRORLEVEL=1 &MSGBUFFER=cal_msg}
end.

if report_yn then do:
   put unformatted skip(2)
      cal_msg skip(1).
end.

assign
   seq_prior_cum_req = 0
   ship_prior_cum_req = ?
   plan_prior_cum_req = ?
   prior_cum_req = ?
   first_ship_date = ?
   last_ship_date = ?.

/*!
sod_conrep:
1  Use ship schedule only
2  Use plan schedule only
3  Replace beginning of plan schedule with the ship schedule
4  Consume logic: replace beginning of plan schedule with ship schedule
and then adjust the last qty in each week of ship schedule data
upward so the cumulative of ship schedule reqs is not less than
the cumulative of plan schedule reqs.
*/

/* PROCESS SEQUENCE SCHEDULES WHEN:        */
/* 1. SEQ. SCHEDULES MODULE ACTIVATED      */
/* 2. ORDER CONTAINS SEQ. SCHEDULES        */
/* 3. UPDATE RSS WITH SEQ. SCHEDULES       */

if using_seq_schedules and so_seq_order and so_inc_in_rss then do:

   /* LOAD SEQUENCE SCHEDULE DATA INTO SHARED   */
   /* WORK-FILE work_seq_sched.                 */
   {gprunmo.i
      &program = ""rcrsupb2.p""
      &module = "ASQ"
      &param = """(input sod_nbr,
                   input sod_line,
                   input seq_rlse_id,
                   output seq_sd_dates)"""}
end.  /* END - PROCESS SEQUENCE SCHEDULE DATA */

if index("134",sod_conrep) > 0 then do:
   /*GET SHIPPING SCHEDULE DATA*/
   find sch_mstr where sch_type = ship_schedule
      and sch_nbr = sod_nbr
      and sch_line = sod_line
      and sch_rlse_id = ship_rlse_id
      no-lock no-error.

   if available sch_mstr then do:
      assign
         ship_sd_dates = sch_sd_dates
         ship_sd_pat = sch_sd_pat
         ship_prior_cum_req = sch_pcr_qty
         prior_cum_req = sch_pcr_qty
         prior_cum_req_date = sch_pcs_date.

      /*! GET SHIPPING SCHEDULE COMMENT INDEX */
      ship-cmtindx = sch_cmtindx.

      if report_yn then do:
         {gprun.i ""rciq1a.p""
            "(input sch_type, input sch_nbr, input sch_line,
              input sch_rlse_id, input no)"}
      end.

      for each schd_det no-lock
         where schd_type    = sch_type
           and schd_nbr     = sch_nbr
           and schd_line    = sch_line
           and schd_rlse_id = sch_rlse_id on endkey undo, return:
         /*! IF A SHIP/DLVY PATTERN CODE, THEN IT MUST CALL FOR
         CONSOLIDATION */

         assign
            ship_dlvy_code = sch_sd_pat
            interval_code = schd_interval
            req_dt = schd_date
            req_qty = schd_discr_qty
            ship_interval = schd_interval.

         {gprun.i ""rcsdpint.p""}

         /*AUTHORIZATION NUMBER PROCESSING*/
         {rcrsupb1.i}

         if resch_stat <> ""
         then do:
            put unformatted skip(2)
               caps(getTermLabel("WARNING",20)) + ": " + resch_stat
               skip
               getTermLabel("ORDER",5) + ": " + schd_nbr + " " +
               getTermLabel("LINE",4) + ": " + string(schd_line)
               skip(1).
            warn_ct = warn_ct + 1.
         end.

         do i = 1 to resch_ct:
            find first work_rqm no-lock no-error.
            create work_ship_schd.
            assign
               work_ship_date = resch_dt[i]
               work_ship_time = schd_time
               work_ship_reference =
                  (if available work_rqm then work_rqm_value
                   else schd_reference)
               work_ship_rlse_id = sch_rlse_id
               work_ship_recid = recid(schd_det)
               work_ship_qty = resch_qty[i]
               work_ship_cmtindx = schd_cmtindx.
            if available work_rqm
               then delete work_rqm.

            recno = recid(work_ship_schd).

            if first_ship_date = ? or work_ship_date < first_ship_date
            then
               first_ship_date = work_ship_date.

            if last_ship_date = ? or work_ship_date > last_ship_date
            then
               last_ship_date = work_ship_date.

            if report_yn and ship_dlvy_code <> "" then do:
               display
                  work_ship_date
                  work_ship_time
                  work_ship_reference
                  work_ship_qty
               with frame shipsdp STREAM-IO /*GUI*/ .
               down 1 with frame shipsdp.
            end.
         end.
      end.  /* for each schd_det */
   end.  /* if available sch_mstr */
   else do:
      /* Warning: No Shipping Schedule found */
      {pxmsg.i &MSGNUM=4551 &ERRORLEVEL=2 &MSGBUFFER=l_msg}

      put unformatted skip(1)
         l_msg
         skip
         getTermLabel("ORDER",5) + ": " + sod_nbr +
         getTermLabel("LINE",4) + ": " + string(sod_line)
         skip(1).
      warn_ct = warn_ct + 1.

      /* FOR NETTING LOGIC 3 AND 4, CREATE REQUIRED SHIP SCHEDULE */
      /* IF PLAN AND/OR SHIP SCHEDULE EXISTS                      */

      if sod_conrep = "1" or
         (index("34",sod_conrep) > 0 and
         not can-find (sch_mstr where sch_type    = plan_schedule and
                                      sch_nbr     = sod_nbr       and
                                      sch_line    = sod_line      and
                                      sch_rlse_id = plan_rlse_id))
      then
         undo,leave.

   end.

   /*! SET CUM QTIES AND MONDAY DATES (OF THE MONDAY  THRU  SUNDAY
   WEEK THAT THE SCHEDULE DATE IS IN) */

   if using_seq_schedules or
      index("34",sod_conrep) > 0 then do:

      temp_ship_prior_cum_req = ship_prior_cum_req.

      for each work_ship_schd
         by work_ship_date
         by work_ship_time
         by work_ship_reference:
         assign
            temp_ship_prior_cum_req = temp_ship_prior_cum_req + work_ship_qty
            work_ship_cum_qty       = temp_ship_prior_cum_req.

         {gprun.i ""rswmd.p""
            "(input work_ship_date, input yes,
              output work_ship_week_date)"}

      end.  /* for each work_ship_schd */
   end.  /* if using_seq_schedules  */

end.  /* if index("134",sod_conrep) > 0 */

last_plan_week_date = ?.

if index("234",sod_conrep) > 0 then do:
   /*GET PLANNING SCHEDULE DATA */
   find sch_mstr where sch_type = plan_schedule
      and sch_nbr = sod_nbr
      and sch_line = sod_line
      and sch_rlse_id = plan_rlse_id
      no-lock no-error.

   if available sch_mstr then do:
      assign
         plan_sd_dates = sch_sd_dates
         plan_sd_pat = sch_sd_pat
         plan_prior_cum_req = sch_pcr_qty.

      /*! GET PLANNING SCHEDULE COMMENT INDEX */
      plan-cmtindx = sch_cmtindx.

      if prior_cum_req = ?
      then
         assign
            prior_cum_req      = sch_pcr_qty
            prior_cum_req_date = sch_pcs_date.

      if report_yn then do:
         {gprun.i ""rciq1a.p""
            "(input sch_type, input sch_nbr, input sch_line,
              input sch_rlse_id, input no)"}
      end.

      /*! GET PLANNING SCHEDULE DATA - DISCRETE OR WEEKLY QUANTITIES */

      for each schd_det no-lock
         where schd_type = sch_type
           and schd_nbr = sch_nbr
           and schd_line = sch_line
           and schd_rlse_id = sch_rlse_id
           and (schd_interval = "d" or schd_interval = ""
            or schd_interval = "w")
         break by schd_date by schd_time:

         assign
            last_plan_week_date = schd_date
            ship_dlvy_code = sch_sd_pat
            interval_code = schd_interval
            req_dt = schd_date
            req_qty = schd_discr_qty.

         {gprun.i ""rcsdpint.p""}

         /*AUTHORIZATION NUMBER PROCESSING*/
         {rcrsupb1.i}

         if resch_stat <> "" then do:
            put unformatted skip(2)
               caps(getTermLabel("WARNING",20)) + ": " + resch_stat
               skip
               getTermLabel("ORDER",5) + ": " + schd_nbr +
               getTermLabel("LINE",4) + ": " + string(schd_line)
               skip(1).
            warn_ct = warn_ct + 1.
         end.

         do i = 1 to resch_ct:
            find first work_rqm no-lock no-error.
            create work_plan_schd.
            assign
               work_plan_date = resch_dt[i]
               work_plan_time = schd_time
               work_plan_reference =
                  (if available work_rqm then work_rqm_value
                   else schd_reference)
               work_plan_recid = recid(schd_det)
               work_plan_rlse_id = sch_rlse_id
               work_plan_qty = resch_qty[i]
               work_plan_cmtindx = schd_cmtindx.
            if available work_rqm
               then delete work_rqm.

            recno = recid(work_plan_schd).

            /* DETERMINE FIRST & LAST DATES OF THE SCHEDULE FOR */
            /* COMPARISON WITH SEQUENCE SCHEDULES.              */
            if first_plan_date = ? or
               work_plan_date < first_plan_date then
               first_plan_date = work_plan_date.

            if last_plan_date = ? or
               work_plan_date > last_plan_date then
               last_plan_date = work_plan_date.

            if report_yn and ship_dlvy_code <> "" then do:
               display
                  work_plan_date
                  work_plan_time
                  work_plan_reference
                  work_plan_qty
                  schd_interval    column-label "Original!Interval"
                  working_hours[i] column-label "Ship-to!Working!Hours"
                                   format ">9.9"
               with frame plansdp STREAM-IO /*GUI*/ .
               down 1 with frame plansdp.
            end.
         end.  /* do i = 1 to resch_ct */
      end.  /* for each schd_det  */

      /*! GET PLANNING SCHEDULE DATA - MONTHLY QUANTITIES */

      for each schd_det no-lock
         where schd_type = sch_type
           and schd_nbr = sch_nbr
           and schd_line = sch_line
           and schd_rlse_id = sch_rlse_id
           and schd_interval = "m"
         break by schd_date by schd_time:

         do for next_schd_det:
            find first next_schd_det
               where schd_type = sch_type
               and schd_nbr = sch_nbr
               and schd_line = sch_line
               and schd_rlse_id = sch_rlse_id
               and schd_date > schd_det.schd_date
               and schd_interval = "m"
               no-lock no-error.
            if available next_schd_det then do:
               i = (next_schd_det.schd_date - schd_det.schd_date) / 7.
               if i = 0 then i = 1.
               if i > 6 then i = 4.
            end.
            else
               i = 4.

         end.

         /*! DIVIDE THE QUANTITIES WITH RUNNING ROUND */
         assign
            resch_ct = i
            iweeks   = i
            req_qty  = schd_discr_qty.
         {gprun.i ""rcsdpnta.p""}
         do i = 1 to resch_ct:
            temp_qty[i] = resch_qty[i].
         end.

         /*! GO THRU THE WEEKS IN  THE  MONTH  AND  INTERPRET  PER
         SHIP/DELIVERY PATTERN */

         temp_date = schd_date.

         do ii = 1 to iweeks:

            assign
               ship_dlvy_code = sch_sd_pat
               interval_code  = "w"
               req_dt         = temp_date.

            /* THE FOLLOWING LINE IS BECAUSE IF WE HAVE A
            "MONTHLY" SHIP/DLVY PATTERN CODE, WE DON'T WANT TO
            USE THE DERIVED WEEK DATE. */
            if sch_sd_pat begins "0"
            then
               assign
                  req_dt        = schd_date
                  interval_code = "m".

            req_qty = temp_qty[ii].
            {gprun.i ""rcsdpint.p""}

            /*AUTHORIZATION NUMBER PROCESSING*/
            {rcrsupb1.i}

            if resch_stat <> "" then do:
               put unformatted skip(2)
                  caps(getTermLabel("WARNING",20)) + ": " + resch_stat
                  skip
                  getTermLabel("ORDER",5) + ": " + schd_nbr +
                  getTermLabel("LINE",4) + ": " + string(schd_line)
                  skip(1).
               warn_ct = warn_ct + 1.
            end.

            do i = 1 to resch_ct:
               find first work_rqm no-lock no-error.

               /* IF WEEKLY OR DISCRETE PLAN SCHEDULES EXIST FOR */
               /* THE SAME DATE AS ONE OF THE WEEKLY INCREMENTS  */
               /* FROM A MONTHLY SCHEDULE THEN DO NOT CONSIDER   */
               /* THAT WEEKLY INCREMENT                          */

               find first work_plan_schd where
                  work_plan_date      = resch_dt[i] and
                  work_plan_interval <> "m"  no-lock no-error.
               if available work_plan_schd then next.

               create work_plan_schd.
               assign
                  work_plan_date = resch_dt[i]
                  work_plan_time = ""
                  work_plan_reference =
                     (if available work_rqm then work_rqm_value
                      else schd_reference)
                  work_plan_recid = recid(schd_det)
                  work_plan_rlse_id = sch_rlse_id
                  work_plan_qty = resch_qty[i]
                  work_plan_interval = "m"
                  work_plan_cmtindx = schd_cmtindx.
               if available work_rqm
                  then delete work_rqm.

               recno = recid(work_plan_schd).

               /* DETERMINE FIRST & LAST DATES OF THE SCHEDULE FOR */
               /* COMPARISON WITH SEQUENCE SCHEDULES.              */
               if first_plan_date = ? or
                  work_plan_date < first_plan_date then
                  first_plan_date = work_plan_date.

               if last_plan_date = ? or
                  work_plan_date > last_plan_date then
                  last_plan_date = work_plan_date.

               if report_yn and ship_dlvy_code <> "" then do:
                  display
                     work_plan_date
                     work_plan_time
                     work_plan_reference
                     work_plan_qty
                     schd_interval    column-label "Original!Interval"
                     working_hours[i] column-label "Ship-to!Working!Hours"
                                      format ">9.9"
                  with frame plansdp STREAM-IO /*GUI*/ .

                  down 1 with frame plansdp.
               end.
            end.

            temp_date = temp_date + 7.
         end.  /* do ii = 1 to iweeks */
      end.  /* for each schd_det */
   end.  /* if available sch_mstr */
   else do:
      /* Warning: No Planning Schedule Found */
      {pxmsg.i &MSGNUM=4552 &ERRORLEVEL=2 &MSGBUFFER=l_msg}

      put unformatted skip(1)
         l_msg
         skip
         getTermLabel("ORDER",5) + ": " + sod_nbr +
         getTermLabel("LINE",4) + ": " + string(sod_line)
         skip(1).
      warn_ct = warn_ct + 1.
      if sod_conrep = "2" then
         undo, leave.
   end.

   /*! SET CUM QTIES AND MONDAY DATES (OF THE MONDAY  THRU  SUNDAY */
   /*  WEEK THAT THE SCHEDULE DATE IS IN) */

   if using_seq_schedules or
      index("34",sod_conrep) > 0 then do:

      temp_plan_prior_cum_req = plan_prior_cum_req.

      for each work_plan_schd
         by work_plan_date
         by work_plan_time
         by work_plan_reference:
         assign
            temp_plan_prior_cum_req = temp_plan_prior_cum_req + work_plan_qty
            work_plan_cum_qty = temp_plan_prior_cum_req.

         {gprun.i ""rswmd.p"" "(input work_plan_date, input yes,
                                output work_plan_week_date)"}

      end.  /* for each work_plan_schd  */
   end.  /* if using_seq_schedules */

end.   /* if index("134",sod_conrep) > 0 */

run get_ship_plan_pcr (output ship_plan_pcr).

/* DISPLAY NETTING LOGIC OPTION */
for first code_mstr where
   code_fldname = "sod_conrep" and
   code_value   = sod_conrep
   no-lock:
end.
if available(code_mstr) and report_yn then
   if sod_conrep = "3" then
      display
         sod_conrep
         code_cmmt
         ship_plan_pcr
      with frame net-logic-pcr STREAM-IO /*GUI*/ .
   else
      display
         sod_conrep
         code_cmmt
      with frame net-logic STREAM-IO /*GUI*/ .

/* SCAN THE SEQUENCE WORKFILE TO BUILD A LIST OF PARAMETERS */

if using_seq_schedules and so_seq_order and so_inc_in_rss and
   can-find (first work_seq_schd) then
   run process_seq_schedule_data
      (output last_seq_date,
       output last_seq_week_date,
       output seq_cumulative,
       output seq_weekly_cum,
       output seq_end_of_week,
       output seq_next_open_date).

/* REPLACE LOGIC ----->  REPLACE SHIPPING WITH SEQUENCE */

if index("134",sod_conrep) > 0 then
if using_seq_schedules and so_seq_order and so_inc_in_rss and
   can-find (first work_seq_schd) then

   /* ADJUST THE BEGINNING OF THE SHIP SCHEDULE WITH THE */
   /* QUANTITIES FROM THE SEQUENCE SCHEDULE.             */

   /* REPLACED MFC_LOGICAL WITH SHIP_PLAN_PCR */
   run replace_ship_with_seq_schedule
      (input plan_sd_pat,     /* SHIP DELIVERY PATTERN */
       input ship_plan_pcr,   /* USE SHIP/PLAN PCR     */
       input sod_conrep,      /* NETTING LOGIC OPTION  */
       input ship_prior_cum_req,
       input last_seq_date,
       input last_seq_week_date,
       input seq_cumulative,
       input seq_weekly_cum,
       input seq_end_of_week,
       input seq_next_open_date,
       output first_ship_cumulative,
       output first_ship_week_date).

/* REPLACE LOGIC ----->  REPLACE PLANNING WITH SEQUENCE */

if index("23",sod_conrep) > 0 then
   if using_seq_schedules and so_seq_order and so_inc_in_rss and
      can-find (first work_seq_schd) then

   /* ADJUST THE BEGINNING OF THE PLAN SCHEDULE WITH THE */
   /* QUANTITIES FROM THE SEQUENCE SCHEDULE.  STOP WHEN  */
   /* FIRST WEEK OF THE SHIP SCHEDULE IS REACHED.        */

   /* REPLACED MFC_LOGICAL WITH SHIP_PLAN_PCR */
   run replace_plan_with_seq_schedule
      (input plan_sd_pat,     /* SHIP DELIVERY PATTERN */
       input ship_plan_pcr,   /* USE SHIP/PLAN PCR     */
       input sod_conrep,      /* NETTING LOGIC OPTION  */
       input first_ship_week_date,
       input plan_prior_cum_req,
       input last_seq_date,
       input last_seq_week_date,
       input seq_cumulative,
       input seq_weekly_cum,
       input seq_end_of_week,
       input seq_next_open_date,
       output first_plan_cumulative).

/* REPLACE LOGIC ----->  REPLACE PLANNING WITH SHIPPING */

/* WHEN SHIP/PLAN PCR IS SET TO YES, MOVE THE REMAINING */
/* QUANTITIES FROM THE PLAN SCHEDULE TO THE WEEK        */
/* FOLLOWING THE LAST SHIP SCHEDULE.                    */
if  sod_conrep = "3"
and ship_plan_pcr
then do:

   /* DO NOT CONSIDER PLANNED REQ IF THEY ARE BEFORE LAST SHIP DATE OR     */
   /* CUMULATIVE QUANTIY IS LESS THAN SHIP CUMUMULATIVE. ADJUST FIRST PLAN */
   /* FOR SHIP CUMULATIVE QUANTITY                                         */

   if ship_prior_cum_req <> ? and
      plan_prior_cum_req <> ? then do:

      assign
         l_ship_cumulative = ship_prior_cum_req
         l_plan_cumulative = plan_prior_cum_req.

      /* ADJUST LAST WEEK CUM FOR SEQUENCE SCHEDULES */
      if using_seq_schedules and so_seq_order and so_inc_in_rss then
      assign
         l_ship_cumulative = first_ship_cumulative
         l_plan_cumulative = first_plan_cumulative.

      /* ADJUST THE BEGINNING OF THE PLAN SCHEDULE WITH THE */
      /* QUANTITIES FROM THE SHIPPING SCHEDULE.             */

      run replace_plan_with_ship_schedule
         (input l_plan_cumulative,
         input l_ship_cumulative,
         input plan_sd_pat,     /* SHIP DELIVERY PATTERN */
         input ship_plan_pcr,   /* USE SHIP/PLAN PCR     */
         input last_seq_date,
         input last_seq_week_date,
         input seq_cumulative,
         input seq_weekly_cum,
         input seq_end_of_week,
         input seq_next_open_date).

   end. /* if ship, plan qty and ship date available */

end.  /* if sod_conrep = "3" */

/*! CONSUME LOGIC */

if sod_conrep = "4" then do:

   /* CONSUME LOGIC ----->  CONSUME PLANNING WITH SEQUENCE */

   if using_seq_schedules and so_seq_order and so_inc_in_rss and
      can-find (first work_seq_schd) then

   /* START BY CONSUMING THE PLAN SCHEDULE WITH THE    */
   /* SEQUENCE SCHEDULE.  STOP WHEN YOU REACH THE      */
   /* FIRST WEEK OF THE SHIP SCHEDULE.                 */
   run consume_plan_with_seq_schedule
      (input last_seq_date,
       input first_ship_week_date,
       input sod_nbr,
       input sod_line,
       output first_plan_cumulative).

   /* CONSUME LOGIC ----->  CONSUME PLANNING WITH SHIPPING */

   /*! GO THRU LAST SHIP RECORD OF EACH WEEK AND COMPARE  TO  LAST
   PLAN  RECORD  OF THAT WEEK.  ADJUST SHIP RECORD IF WE NEED TO.
   TO BRING THE SHIP QTY UP TO THE PLAN QTY.
   */

   assign
      last_week_ship_cum_qty = 0
      last_week_plan_cum_qty = 0.

   /* ADJUST LAST WEEK CUM FOR SEQUENCE SCHEDULES */
   if using_seq_schedules and so_seq_order and so_inc_in_rss then
   assign
      last_week_ship_cum_qty = first_ship_cumulative
      last_week_plan_cum_qty = first_plan_cumulative.

   for each work_ship_schd no-lock
      break
      by work_ship_week_date
      by work_ship_date
      by work_ship_time
      by work_ship_cum_qty:
      if last-of(work_ship_week_date) then do:
         /*WE ARE AT THE LAST SHIP QTY FOR THE WEEK*/
         assign
            i                      = 0
            this_is_last_ship_week = last(work_ship_week_date)
            plan_coverage_qty      = 0.

         for each work_plan_schd no-lock
            where work_plan_week_date <= work_ship_week_date
            break
            by work_plan_date
            by work_plan_time
            by work_plan_cum_qty:

            if this_is_last_ship_week
               and
               (
               work_plan_date > work_ship_date
               or
               (
               work_plan_date = work_ship_date
               and
               work_plan_time > work_ship_time
               )
               )
            then do:
               /*! FOR THE LAST QUANTITY IN THE LAST WEEK  OF  THE
               SHIP  SCHEDULE,  WE  NEED  TO  SEE IF THERE IS ANY
               COVERAGE BY THE PLAN SCHEDULE PAST IT  WITHIN  THE
               SAME  WEEK.  IF SO WE REDUCE THE ADJUSTMENT AMOUNT
               BY IT.  */

               plan_coverage_qty = plan_coverage_qty + work_plan_qty.
            end.

            if last(work_plan_date) then do:

               adjust_qty = (work_plan_cum_qty - last_week_plan_cum_qty
                          - plan_coverage_qty)
                          - (work_ship_cum_qty - last_week_ship_cum_qty).

               /* ADJUST FOR SEQUENCE SCHEDULES */
               /* -- IN THE EVENT THE SEQUENCE, SHIP, PLAN -- */
               /* -- SCHEDULES OVERLAP IN THE SAME WEEK.   -- */

               if using_seq_schedules and
                  so_seq_order and so_inc_in_rss then do:

                  /* DETERMINE WEEKLY QTY FROM SEQUENCE */
                  run determine_weekly_seq_qty
                     (input work_ship_week_date,
                      output last_week_seq_cum_qty,
                      output this_week_seq_cum_qty).

                  /* CALCULATE FURTHER ADJUSTMENT FROM SEQUENCE */
                  adjust_qty = adjust_qty -
                               (this_week_seq_cum_qty -
                               last_week_seq_cum_qty).

               end.  /* if using_seq_schedules */

               new_ship_qty = work_ship_qty + adjust_qty.

               if adjust_qty > 0 then do:
                  if report_yn then do:
                     display
                        work_ship_date
                        work_ship_time
                        work_ship_reference
                        work_ship_qty
                        adjust_qty column-label "Adjustment Qty"
                        new_ship_qty column-label "New Ship Qty"
                     with frame consume_adjust STREAM-IO /*GUI*/ .

                     down 1 with frame consume_adjust.
                  end.

                  work_ship_qty = new_ship_qty.
               end.

               assign
                  i = i + 1
                  last_week_plan_cum_qty = work_plan_cum_qty.

               /* MOVED END STATMENT BELOW TO ADJUST QUANTITY IN THE */
               /* LAST WEEK WHEN PLAN SCHEDULES ARE IN DALIY BUCKETS */

            end.  /* IF LAST(work_plan_date) */

         end.  /*  for each work_plan_schd */

         if i = 0 then do:
            /* Consume logic found no Plan Schedule data for week of */
            {pxmsg.i &MSGNUM=4495 &ERRORLEVEL=1 &MSGBUFFER=l_msg
                     &MSGARG1=work_ship_week_date}
            put unformatted skip(2)
               l_msg
               skip
               getTermLabel("ORDER",5) + ": " + sod_nbr +
               getTermLabel("LINE",4) + ": " + string(sod_line)
               skip(1).
            warn_ct = warn_ct + 1.
         end.
         else
            last_week_ship_cum_qty = work_ship_cum_qty.
      end.  /* if last-of(work_ship_week_date) */

   end.  /* for each work_ship_schd  */
end.  /* if sod_conrep = "4" */

/*! GET RID OF PLAN SCHEDULE RECORDS NOT LATER THAN LAST SHIP
SCHEDULE RECORD */

if last_ship_date <> ? then do:
   for each work_plan_schd exclusive-lock where
         work_plan_date <= last_ship_date:
      delete work_plan_schd.
   end.
end.

/*! ---  START CREATING COMBINED SCHEDULE ---  */

/*! START WITH SEQUENCE SCHEDULE, IF INSTALLED */

if using_seq_schedules and so_seq_order and so_inc_in_rss then do:
   for each work_seq_schd
         where work_seq_qty > 0
         by work_seq_date by work_seq_time:

      create work_schd.
      assign
         work_date = work_seq_date
         work_time = work_seq_time
         work_reference = work_seq_reference
         work_qty = work_seq_qty
         work_type = seq_schedule
         work_recid = work_seq_recid
         work_cmtindx = work_seq_cmtindx
         recno = recid(work_schd).
   end.

end. /* if enable sequenced schedules */

/*! THEN SHIP SCHEDULE */
/*! START CREATING COMBINED SCHEDULE - USE SHIP SCHEDULE FIRST */

for each work_ship_schd
   by work_ship_date by work_ship_time:
   create work_schd.
   assign
      work_date = work_ship_date
      work_time = work_ship_time
      work_reference = work_ship_reference
      work_qty = work_ship_qty
      work_type = ship_schedule
      work_recid = work_ship_recid
      work_cmtindx = work_ship_cmtindx
      recno = recid(work_schd).
end.

/*! THEN PLAN SCHEDULE */

for each work_plan_schd
   by work_plan_date by work_plan_time:
   create work_schd.
   assign
      work_date = work_plan_date
      work_time = work_plan_time
      work_reference = work_plan_reference
      work_qty = work_plan_qty
      work_type = plan_schedule
      work_recid = work_plan_recid
      work_cmtindx = work_plan_cmtindx
      recno = recid(work_schd).
end.

if report_yn then do:

   for each work_schd no-lock
      by work_date by work_time
      by work_type descending
      by work_reference with frame f-a:

      /* SET EXTERNAL LABELS */
      setFrameLabels(frame f-a:handle).

      if work_type = plan_schedule then
         where_from = getTermLabel("PLAN_SCHEDULE",15).
      else
      if work_type = seq_schedule then
         where_from = getTermLabel("SEQ_SCHEDULE",15).
      else
         where_from = getTermLabel("SHIP_SCHEDULE",15).
      display
         work_date
         work_time
         work_reference
         work_qty
         where_from
      with title color normal (getFrameTitle("COMBINED_SCHEDULE",25))  STREAM-IO /*GUI*/ .

   end.
end.

if report_yn then do:
   put unformatted skip(2)
      cal_msg skip(1).
end.

if shipto_calid <> ? then do:
   /*! IF THERE IS A CALENDAR FOR THE SHIPTO OR CUSTOMER, GO  THRU
   THE  WORK-SCHEDULE  AND  BACKWARD-ADJUST  DUE  DATES PER THEIR
   OPERATING DAYS.  HOPEFULLY THEIR SHIP SCHEDULE  IS  PER  THEIR
   CALENDAR  SO  THAT  NO  CHANGE  WOULD BE MADE TO SHIP SCHEDULE
   DATES.  */

   for each work_schd by work_date by work_time:
      {rchdate.i work_date -1 shipto_calid}
   end.

   if report_yn then do:
      for each work_schd no-lock
         break by work_date by work_time
               by work_type descending
               by work_reference with frame f-b:

         /* SET EXTERNAL LABELS */
         setFrameLabels(frame f-b:handle).

         if work_type = plan_schedule then
            where_from = getTermLabel("PLAN_SCHEDULE",15).
         else
            if work_type = seq_schedule then
               where_from = getTermLabel("SEQ_SCHEDULE",15).
            else
               where_from = getTermLabel("SHIP_SCHEDULE",15).
         display
            work_date
            work_time
            work_reference
            work_qty
            where_from
         with title
         color normal
         (getFrameTitle("SCHED_DATES_BKWD_ADJ_FOR_CUST",78))
         with width 80 STREAM-IO /*GUI*/ .
      end.
   end.
end.

if report_yn then do:

   if using_seq_schedules and
      can-find(first work_schd where work_type = seq_schedule) then
      put unformatted
         skip(2)
         getTermLabel("SEQ_SCHEDULE",15)
         skip
         if seq_sd_dates then getTermLabel("SHIPMENT",8)
         else getTermLabel("DELIVERY",8) space(1)
         getTermLabel("BASED_-_USING_TRANSPORTATION_LT_OF",40) space(1)
         if seq_sd_dates then 0 else so_translt_days space(1)
         getTermLabel("DAYS",4)
         skip(1).

   if can-find(first work_schd where work_type = plan_schedule)
      /* OPTION 1 IS SHIPPING ONLY, REPLACED BY OPTION 2 */
      and index("234",sod_conrep) > 0 then
      put unformatted
         skip(2)
         getTermLabel("PLAN_SCHEDULE",15)
         skip
         if plan_sd_dates then getTermLabel("SHIPMENT",8)
         else getTermLabel("DELIVERY",8) space(1)
         getTermLabel("BASED_-_USING_TRANSPORTATION_LT_OF",40) space(1)
         if plan_sd_dates then 0 else so_translt_days space(1)
         getTermLabel("DAYS",4)
         skip(1).

   if can-find(first work_schd where work_type = ship_schedule)
      /* OPTION 2 IS PLANNING ONLY, REPLACED BY OPTION 1 */
      and index("134",sod_conrep) > 0 then
      put unformatted
         skip(2)
         getTermLabel("SHIP_SCHEDULE",15)
         skip
         if ship_sd_dates then getTermLabel("SHIPMENT",8)
         else getTermLabel("DELIVERY",8) space(1)
         getTermLabel("BASED_-_USING_TRANSPORTATION_LT_OF",40) space(1)
         if ship_sd_dates then 0 else so_translt_days space(1)
         getTermLabel("DAYS",4)
         skip(1).

end. /* if report_yn */

for each work_schd by work_date by work_time
   by work_reference:

   assign
      start_date = work_date
      so_translt_days = decimal(substring(so_conrep,2,6))
      so_translt_days = if work_type = seq_schedule and seq_sd_dates
                        then 0 else so_translt_days
      so_translt_days = if work_type = plan_schedule and plan_sd_dates
                        then 0 else so_translt_days
      so_translt_days = if work_type = ship_schedule and ship_sd_dates
                        then 0 else so_translt_days.

   {gprun.i ""rcshipdt.p""
      "(input sod_site,
        input shipto_calid,
        input work_date,
        input work_time,
        input so_translt_days,
        output shipfrom_date,
        output shipfrom_time,
        output arrive_date,
        output arrive_time,
        output ret_status)"}

   if ret_status <> "" then do:
      put unformatted skip(2)
         caps(getTermLabel("WARNING",20)) + ":" space(1) ret_status
         space(1)
         work_date
         skip
         getTermLabel("ORDER",5) + ": " + sod_nbr +
         getTermLabel("LINE",4) + ": " + string(sod_line)
         skip(1).
      warn_ct = warn_ct + 1.
   end.

   assign
      work_date = shipfrom_date
      work_time = shipfrom_time.
end.

if report_yn then do:
   if can-find(shop_cal where shop_site = sod_site and shop_wkctr = ""
      and shop_mch = "") then do:
      shipfrom_calid = sod_site.
      if report_yn then do:
         put unformatted skip(2)
            getTermLabel("USING_SHIP-FROM_CALENDAR",45) space(1)
            shipfrom_calid skip(1).
      end.
   end.
   else
   if can-find(shop_cal where shop_site = "" and shop_wkctr = ""
      and shop_mch = "") then do:
      shipfrom_calid = "".
      if report_yn then do:
         put unformatted skip(2)
            getTermLabel("USING_DEFAULT_FACTORY_CALENDAR",45) skip(1).
      end.
   end.
   else do:
      shipfrom_calid = ?.
      if report_yn then do:
         /* No Ship-From or Default Factory calendar was found */
         {pxmsg.i &MSGNUM=4499 &ERRORLEVEL=1 &MSGBUFFER=l_msg}
         put unformatted skip(2)
            l_msg skip(1).
      end.
   end.

   for each work_schd no-lock
      by work_date by work_time
      by work_type descending
      by work_reference with frame f-c:

      /* SET EXTERNAL LABELS */
      setFrameLabels(frame f-c:handle).

      if work_type = plan_schedule then
         where_from = getTermLabel("PLAN_SCHEDULE",15).
      else
      if work_type = seq_schedule then
         where_from = getTermLabel("SEQ_SCHEDULE",15).
      else
         where_from = getTermLabel("SHIP_SCHEDULE",15).
      display
         work_date
         work_time
         work_reference
         work_qty
         where_from
      with title
      color normal
      (getFrameTitle("SCHED_DATES_BKWD_ADJ_FOR_SHIPFROM",78)) width 80 STREAM-IO /*GUI*/ .
   end.
end.

/*! THIS SHOULD DO IT FOR A START, AND  SHOULD  PRETTY  WELL
COVER PLANNING REQUIREMENTS.  THIS COULD GET MORE INVOLVED,
IE SHIPMENT TIMING PER TRADING  PARTNERS'  PLANT  OPERATING
SCHEDULES,  DOES  THE  TRANSPORTER WORK OVERTIME, AND OTHER
BUSINESS RULES PARTICULAR TO THE TRADING PARTNERS.  */

/*! NOW FINALLY, WE CAN DROP IT INTO A NEW REQUIRED SCHEDULE. */

sod_rlse_nbr = sod_rlse_nbr + 1.
if sod_rlse_nbr = 1000000 then sod_rlse_nbr = 1.

do while can-find(sch_mstr where sch_type = req_schedule
   and sch_nbr = sod_nbr and sch_line = sod_line
   and sch_rlse_id = string(sod_rlse_nbr,"999999")):
   sod_rlse_nbr = sod_rlse_nbr + 1.
   if sod_rlse_nbr = 1000000 then sod_rlse_nbr = 1.
end.

create sch_mstr.
assign
   sch_type = req_schedule
   sch_nbr  = sod_nbr
   sch_line = sod_line
   sch_rlse_id = string(sod_rlse_nbr,"999999")
   recno = recid(sch_mstr).

/*! PICK UP SOME DATA ITEMS FROM PRIOR SCHEDULE AND SET END EFFECTIVE */

if sod_curr_rlse_id[req_schedule] > "" then do for prev_sch_mstr:
   find prev_sch_mstr
      where sch_type = sch_mstr.sch_type
        and sch_nbr = sch_mstr.sch_nbr
        and sch_line = sch_mstr.sch_line
        and sch_rlse_id = sod_curr_rlse_id[req_schedule]
      exclusive-lock.

      sch_eff_end = today.

      {gprun.i ""rcsinit.p""
         "(input recid(prev_sch_mstr), input recid(sch_mstr),
           input no)"}

end.

assign
   sch_cr_date = today
   sch_cr_time = time
   sch_from_pid = plan_rlse_id
   sch_from_sid = ship_rlse_id
   sch_pcr_qty = prior_cum_req
   sch_pcs_date = prior_cum_req_date
   sch_eff_start = today
   sch_eff_end = ?.

/*! COPY/APPEND COMMENTS FROM PLAN & SHIP SCHEDULE:
    THE PLAN & SHIP COMMENT INDEXES HAVE ONLY BEEN INITIALIZED
    IF THE CONSUME LOGIC IS SET TO USE THOSE SCHEDULES.  IF BOTH
    ARE USED, COPY SHIP SCHEDULE COMMENTS FIRST.  COMMENTS ARE
    NOT COPIED FROM PREVIOUS RELEASES OF THE REQUIRED SCHEDULE.
*/
if ship-cmtindx <> 0 then do for cmt_det:
   {gpcmtcpy.i  &old_index = ship-cmtindx
                &new_index = new_index &counter = i}
   sch_cmtindx = new_index.
   /* APPEND PLAN COMMENTS TO NEXT PAGE IF THEY EXIST */
   if plan-cmtindx <> 0 then do:
      find last cmt_det where cmt_indx = new_index no-lock no-error.
      last-cmtseq = cmt_seq + 1.
      for each cmt_det where cmt_indx = plan-cmtindx no-lock:
         outside:
         repeat:
            do on error undo, leave:
               create cmtdet.
               assign
                  cmtdet.cmt_indx  = new_index
                  cmtdet.cmt_seq   = last-cmtseq
                  cmtdet.cmt_print = cmt_det.cmt_print
                  cmtdet.cmt_type  = cmt_det.cmt_type
                  cmtdet.cmt_ref   = cmt_det.cmt_ref
                  cmtdet.cmt_lang  = cmt_det.cmt_lang
                  cmtdet.cmt_user1 = cmt_det.cmt_user1
                  cmtdet.cmt_user2 = cmt_det.cmt_user2.
               do i = 1 to 15:
                  cmtdet.cmt_cmmt[i] = cmt_det.cmt_cmmt[i].
               end.
               last-cmtseq = last-cmtseq + 1.
               leave outside.
            end.
         end.
      end. /* for each cmt */
   end.
end.
else if plan-cmtindx <> 0 then do for cmt_det:
      {gpcmtcpy.i  &old_index = plan-cmtindx
               &new_index = new_index &counter = i}
      sch_cmtindx = new_index.
end.

/* HERE WE CREATE OUR NEW REQUIRED SCHEDULE DETAIL */
for each work_schd no-lock
   break by work_date by work_time
   by work_reference:
   if first-of(work_reference) then temp_cmtindx = 0.

   if work_type = 2 then temp_cmtindx = work_cmtindx.

   accumulate work_qty (sub-total by work_reference).
   if last-of(work_reference) then do:
      create schd_det.
      assign
         schd_type = sch_type
         schd_nbr = sch_nbr
         schd_line = sch_line
         schd_rlse_id = sch_rlse_id
         schd_date = work_date
         schd_time = work_time
         schd_interval = "D"
         substring(schd_fc_qual,1,1) = substring(string(work_type),1,1)
         schd_reference = work_reference
         recno = recid(schd_det)
         schd_discr_qty = accum sub-total by work_reference work_qty.

      /*! COPY COMMENT DATA IF ANY.  THE WAY THIS WORKS
         WE COPY SHIP SCHEDULE COMMENTS IF WE HAPPEN TO GET
         COINCIDING SHIP AND PLAN SCHEDULE RECORDS. */

      if temp_cmtindx = 0 then temp_cmtindx = work_cmtindx.

      if temp_cmtindx <> 0 then do:
         {gpcmtcpy.i  &old_index = temp_cmtindx
            &new_index = new_index &counter = i}
         schd_cmtindx = new_index.
      end.

      /* SEQUENCE SCHEDULES DO NOT HAVE RQM_DET RECORDS,   */
      /* SO SKIP THIS SECTION FOR SEQUENCE SCHEDULES.*/
      if work_type <> seq_schedule then do:

         find ship_det
         where recid(ship_det) = work_recid
         no-lock.

         /* COPY RQM_DET FROM PLAN/SHIP SCHEDULE TO RSS */
         for each rqm_det
            where rqm_type      = ship_det.schd_type
              and rqm_nbr       = ship_det.schd_nbr
              and rqm_line      = ship_det.schd_line
              and rqm_rlse_id   = ship_det.schd_rlse_id
              and rqm_date      = ship_det.schd_date
              and rqm_time      = ship_det.schd_time
              and rqm_interval  = ship_det.schd_interval
              and rqm_reference = ship_det.schd_reference
            no-lock:

            if (rqm_cat = "AUTHNBR"
               and rqm_value = schd_det.schd_reference)
               or (rqm_cat <> "AUTHNBR") then
            do:

               {gprun.i ""rcrqinit.p"" "(input recid(rqm_det),
                                         input recid(schd_det))"}

            end.
         end.

      end.  /* if work_type <> seq_schedule */
   end.
end.  /* for each work_schd */

/* MERGE ACTIVE SCHEDULE WITH NEW ACTIVE SCHEDULE -BEGIN*/
{gprun.i ""rcrsupc.p"" "(input recid(sod_det),
                         input sch_rlse_id,
                         output undo_status)"}

if undo_status then
   undo, return.
/* MERGE ACTIVE SCHEDULE WITH NEW ACTIVE SCHEDULE -END*/

/*! ORDER MULTIPLE CALCULATIONS/UPDATE CUM QUANTITIES */

{gprun.i ""rcordmlt.p"" "(input recid(sch_mstr),
                          input sod_ord_mult,
                          input report_yn)"}

/*! REBUCKET THE STUFF */

{gprun.i ""rcbucket.p"" "(input recid(sch_mstr), input report_yn)"}

/* BACK OUT THE OLD ACTIVE SCHEDULE FORECAST DATA */
/*ADDED PARAMETER IS_REQ_SCHD_MAINT TO INDICATE THE CALLING FUNCTION */

{gprun.i ""rcrsfc.p"" "(input recid(sod_det),
                        input ""remove"",
                        input is_req_schd_maint)"}

assign
   sod_curr_rlse_id[req_schedule] = sch_rlse_id
   sod_sched_chgd = no.

if update_yn then do:
   /*! UPDATE REMOTE DATABASE SCHEDULE */
   {gprun.i ""rcrshup.p"" "(input recid(sch_mstr))"}

   /* UPDATE SO DETAIL RECORD IN REMOTE DATABASE IF THERE IS ONE*/
   {gprun.i ""rcrsdup.p"" "(input recid(sod_det))"}

   /* UPDATE FORECAST WITH NEW ACTIVE SCHEDULE DATA */
   /* ADDED PARAMETER IS_REQ_SCHD_MAINT TO INDICATE THE CALLING */
   /* FUNCTION */

   {gprun.i ""rcrsfc.p"" "(input recid(sod_det),
                           input ""add"",
                           input is_req_schd_maint)"}

   /* UPDATE MRP */
   {gprun.i ""rcmrw.p"" "(input sod_nbr, input sod_line, input yes)"}
end.

/*! DISPLAY NEW REQ SHIP SCHEDULE */

if report_yn then do:

   put skip(3)
   {gplblfmt.i
      &FUNC=getTermLabel(""NEW_ACTIVE_REQD_SHIP_SCHEDULE_REL"",45)
      &CONCAT="':'"}
   skip(1).
   {gprun.i ""rciq1a.p""
         "(input sch_type, input sch_nbr, input sch_line,
           input sch_rlse_id, input no)"}
end.

if not update_yn then undo, leave.

/* *********************************************** */
/* ------------- INTERNAL PROCEDURES ------------- */
/* *********************************************** */

PROCEDURE determine_weekly_seq_qty:
   define input parameter in_work_ship_week_date
      like schd_date no-undo.
   define output parameter out_last_week_seq_cum_qty
      like schd_discr_qty no-undo.
   define output parameter out_this_week_seq_cum_qty
      like schd_discr_qty no-undo.

   for each work_seq_schd no-lock
      where work_seq_week_date = in_work_ship_week_date
      break
      by work_seq_date
      by work_seq_time
      by work_seq_cum_qty:

      if first-of (work_seq_date) then
         out_last_week_seq_cum_qty = work_seq_cum_qty - work_seq_qty.

      if last-of (work_seq_date) then
         out_this_week_seq_cum_qty = work_seq_cum_qty.

   end.  /* for each work_seq_schd */
END PROCEDURE.  /* calculate_weekly_seq_qty */

PROCEDURE consume_plan_with_seq_schedule:
   define input parameter in_last_seq_date
      like last_seq_date no-undo.
   define input parameter in_first_ship_week_date
      like last_ship_date no-undo.
   define input parameter in_sod_nbr like sod_nbr no-undo.
   define input parameter in_sod_line like sod_line no-undo.

   define output parameter out_first_plan_cumulative
      like schd_cum_qty no-undo.

   define variable last_week_seq_cum_qty
      like schd_discr_qty no-undo.
   define variable last_week_plan_cum_qty
      like schd_discr_qty no-undo.
   define variable new_seq_qty like schd_discr_qty no-undo.
   define variable plan_coverage_qty like schd_cum_qty no-undo.
   define variable this_is_last_seq_week like mfc_logical no-undo.

   /*! GO THRU LAST SEQ. RECORD OF EACH WEEK AND COMPARE  TO  LAST
   PLAN  RECORD  OF THAT WEEK.  ADJUST SEQ. RECORD IF WE NEED TO.
   TO BRING THE SEQ. QTY UP TO THE PLAN QTY.
   */

   assign
      last_week_seq_cum_qty = 0
      last_week_plan_cum_qty = 0.

   /* GET THE SEQUENCES FOR ALL WEEKS UP TO WHERE  */
   /* SHIPPING SCHEDULE STARTS.                    */

   /* IF NO SHIP SCHEDULE THEN DON'T SET UPPER LIMIT. */
   if in_first_ship_week_date = ? then
      in_first_ship_week_date = hi_date.

   for each work_seq_schd no-lock
      where work_seq_week_date < in_first_ship_week_date
      break
      by work_seq_week_date
      by work_seq_date
      by work_seq_time
      by work_seq_cum_qty:
      if last-of(work_seq_week_date) then do:
         /*WE ARE AT THE LAST SEQ QTY FOR THE WEEK*/
         assign
            i = 0
            this_is_last_seq_week = last(work_seq_week_date)
            plan_coverage_qty = 0.

         for each work_plan_schd no-lock
            where work_plan_week_date = work_seq_week_date
            break
            by work_plan_date
            by work_plan_time
            by work_plan_cum_qty:

            if this_is_last_seq_week
            and
            (
            work_plan_date > work_seq_date
            or
            (
            work_plan_date = work_seq_date
            and
            work_plan_time > work_seq_time
            )
            )
            then do:
               /*! FOR THE LAST QUANTITY IN THE LAST WEEK  OF  THE
                   SEQ  SCHEDULE,  WE  NEED  TO  SEE IF THERE IS ANY
                   COVERAGE BY THE PLAN SCHEDULE PAST IT  WITHIN  THE
                   SAME  WEEK.  IF SO WE REDUCE THE ADJUSTMENT AMOUNT
                   BY IT.  */

               plan_coverage_qty = plan_coverage_qty + work_plan_qty.
            end.

            if last(work_plan_date) then do:

               assign
                  adjust_qty = (work_plan_cum_qty
                                - last_week_plan_cum_qty
                                - plan_coverage_qty)
                                - (work_seq_cum_qty
                                - last_week_seq_cum_qty)
                  new_seq_qty = work_seq_qty + adjust_qty.

               if adjust_qty > 0 then do:
                  if report_yn then do:
                     display
                        work_seq_date
                        work_seq_time
                        work_seq_reference
                        work_seq_qty
                        adjust_qty column-label "Adjustment Qty"
                        new_seq_qty column-label "New Ship Qty"
                     with frame seq_consume_adjust STREAM-IO /*GUI*/ .

                     down 1 with frame seq_consume_adjust.
                  end.

                  work_seq_qty = new_seq_qty.
               end.
            end.

            assign
               i = i + 1
               last_week_plan_cum_qty = work_plan_cum_qty.
         end.

         if i = 0 then do:
            /* WARNING: CONSUME LOGIC FOUND NO PLANNING SCHEDULE    */
            {pxmsg.i &MSGNUM=4495 &ERRORLEVEL=1 &MSGBUFFER=l_msg
                     &MSGARG1=work_seq_week_date}
            put unformatted skip(2)
               l_msg
               skip
               getTermLabel("ORDER",5) + ": " + in_sod_nbr +
               getTermLabel("LINE",4) + ": " + string(in_sod_line)
               skip(1).
            warn_ct = warn_ct + 1.
         end.
         else
            last_week_seq_cum_qty = work_seq_cum_qty.
      end.

   end.  /* for each work_seq_schd no-lock */

   /*! GET RID OF PLAN SCHEDULE RECORDS NOT LATER THAN LAST SEQ
       SCHEDULE RECORD */

   if in_last_seq_date <> ? then do:
      for each work_plan_schd exclusive-lock where
         work_plan_date <= in_last_seq_date and
         work_plan_week_date < in_first_ship_week_date:
         out_first_plan_cumulative = work_plan_cum_qty.
         delete work_plan_schd.
      end.
   end.

END PROCEDURE.  /* consume_plan_with_seq_schedule */

PROCEDURE process_seq_schedule_data:
   define output parameter out_last_seq_date like schd_date no-undo.
   define output parameter out_last_seq_week_date
      like schd_date no-undo.
   define output parameter out_seq_cumulative
      like schd_cum_qty no-undo.
   define output parameter out_seq_weekly_cum
      like schd_cum_qty no-undo.
   define output parameter out_seq_end_of_week
      like mfc_logical no-undo.
   define output parameter out_next_open_date like schd_date no-undo.

   define variable l_seq_cumulative like schd_cum_qty no-undo.
   define variable l_seq_weekly_cum like schd_cum_qty no-undo.
   define variable l_last_seq_date like schd_date no-undo.
   define variable l_last_seq_time like schd_time no-undo.
   define variable l_last_seq_week_date like schd_date no-undo.
   define variable l_temp_week_date like schd_date no-undo.

   for first so_mstr where so_nbr = sonbr no-lock:
   end.

   assign
      l_seq_cumulative = 0
      l_seq_weekly_cum = 0.

   for each work_seq_schd no-lock
         break by work_seq_week_date
         by work_seq_date
         by work_seq_time
         by work_seq_ref:

      /* CALCULATE TOTAL QUANTITY FOR THE WEEK */
      if first-of(work_seq_week_date) then
         l_seq_weekly_cum = work_seq_qty.
      else
         l_seq_weekly_cum = l_seq_weekly_cum + work_seq_qty.

      /* CALCULATE TOTAL CUMULATIVE */
      l_seq_cumulative = l_seq_cumulative + work_seq_qty.

      /* SAVE LAST DATE & TIME TO COMPARE */
      if l_last_seq_date = ? or
         work_seq_date > l_last_seq_date then
      assign
         l_last_seq_date = work_seq_date
         l_last_seq_time = work_seq_time
         l_last_seq_week_date =
         work_seq_week_date.
      else
      if work_seq_date = l_last_seq_date and
         work_seq_time > l_last_seq_time then
         l_last_seq_time = work_seq_time.

   end. /* for each work_seq_schd */

   assign
      out_last_seq_date       = l_last_seq_date
      out_last_seq_week_date  = l_last_seq_week_date
      out_seq_cumulative      = l_seq_cumulative
      out_seq_weekly_cum      = l_seq_weekly_cum.

   /* DETERMINE IF LAST QTY IS AT THE END-OF-WEEK  */
   /* BY ADDING 1 DAY, ADJUST FOR SHOP CALENDAR    */
   /* AND SEE IF THE WEEK-DATE CHANGES.            */
   /* IF WEEK-DATE CHANGES, THEN THE REQUIREMENT   */
   /* IS ON THE LAST DAY OF THE WEEK.              */
   /* ------- (USED FOR "REPLACE") ---------       */

   out_next_open_date = l_last_seq_date + 1.
   {mfhdate.i out_next_open_date +1 so_site}
   {gprun.i ""rswmd.p"" "(input out_next_open_date, input yes,
                          output l_temp_week_date)"}

   if l_temp_week_date = l_last_seq_week_date
   then
      out_seq_end_of_week = no.      /* NOT END_OF_WEEK */
   else
      out_seq_end_of_week = yes.     /* END_OF_WEEK */

END PROCEDURE.  /* process_seq_schedule_data */

PROCEDURE process_ship_schedule_data:
   define input-output parameter in_out_ship_cumulative
      like schd_cum_qty no-undo.

   define output parameter out_last_ship_date like schd_date no-undo.
   define output parameter out_last_ship_week_date
      like schd_date no-undo.
   define output parameter out_ship_weekly_cum
      like schd_cum_qty no-undo.
   define output parameter out_ship_end_of_week
      like mfc_logical no-undo.
   define output parameter out_next_open_date like schd_date no-undo.

   define variable l_ship_cumulative like schd_cum_qty no-undo.
   define variable l_ship_weekly_cum like schd_cum_qty no-undo.
   define variable l_last_ship_date like schd_date no-undo.
   define variable l_last_ship_time like schd_time no-undo.
   define variable l_last_ship_week_date like schd_date no-undo.
   define variable l_temp_week_date like schd_date no-undo.

   for first so_mstr where so_nbr = sonbr no-lock:
   end.

   assign
      l_ship_cumulative = in_out_ship_cumulative
      l_ship_weekly_cum = in_out_ship_cumulative.

   for each work_ship_schd no-lock
         break by work_ship_week_date
         by work_ship_date
         by work_ship_time
         by work_ship_ref:

      /* CALCULATE TOTAL QUANTITY FOR THE WEEK */
      if first-of(work_ship_week_date) then
         l_ship_weekly_cum = work_ship_qty.
      else
         l_ship_weekly_cum = l_ship_weekly_cum + work_ship_qty.

      /* CALCULATE TOTAL CUMULATIVE */
      l_ship_cumulative = l_ship_cumulative + work_ship_qty.

      /* SAVE LAST DATE & TIME TO COMPARE */
      if l_last_ship_date = ? or
         work_ship_date > l_last_ship_date then
      assign
         l_last_ship_date = work_ship_date
         l_last_ship_time = work_ship_time
         l_last_ship_week_date = work_ship_week_date.
      else
      if work_ship_date = l_last_ship_date and
         work_ship_time > l_last_ship_time then
         l_last_ship_time = work_ship_time.

   end. /* for each work_ship_schd */

   assign
      out_last_ship_date       = l_last_ship_date
      out_last_ship_week_date  = l_last_ship_week_date
      in_out_ship_cumulative   = l_ship_cumulative
      out_ship_weekly_cum      = l_ship_weekly_cum.

   /* DETERMINE IF LAST QTY IS AT THE END-OF-WEEK  */
   /* BY ADDING 1 DAY, ADJUST FOR SHOP CALENDAR    */
   /* AND SEE IF THE WEEK-DATE CHANGES.            */
   /* IF WEEK-DATE CHANGES, THEN THE REQUIREMENT   */
   /* IS ON THE LAST DAY OF THE WEEK.              */
   /* ------- (USED FOR "REPLACE") ---------       */

   out_next_open_date = l_last_ship_date + 1.
   {mfhdate.i out_next_open_date +1 so_site}
   {gprun.i ""rswmd.p"" "(input out_next_open_date, input yes,
                          output l_temp_week_date)"}

   if l_temp_week_date = l_last_ship_week_date
   then
      out_ship_end_of_week = no.      /* NOT END_OF_WEEK */
   else
      out_ship_end_of_week = yes.     /* END_OF_WEEK */

END PROCEDURE.  /* process_ship_schedule_data */

PROCEDURE replace_plan_with_seq_schedule:
   define input parameter in_plan_sd_pat like sch_sd_pat no-undo.
   define input parameter in_use_ship_plan_pcr
      like mfc_logical no-undo.
   define input parameter in_netting_logic like sod_conrep no-undo.
   define input parameter in_first_ship_week_date
      like schd_date no-undo.
   define input parameter in_plan_prior_cum_req
      like schd_cum_qty no-undo.
   define input parameter in_last_seq_date like schd_date no-undo.
   define input parameter in_last_seq_week_date
      like schd_date no-undo.
   define input parameter in_seq_cumulative
      like schd_cum_qty no-undo.
   define input parameter in_seq_weekly_cum
      like schd_cum_qty no-undo.
   define input parameter in_seq_end_of_week
      like mfc_logical no-undo.
   define input parameter in_next_open_date
      like schd_date no-undo.

   define output parameter out_first_plan_cumulative
      like schd_cum_qty no-undo.

   define variable l_plan_cumulative like schd_cum_qty no-undo.
   define variable l_first_plan_record like mfc_logical no-undo
      initial yes.

   if plan_prior_cum_req <> ? then
      l_plan_cumulative = in_plan_prior_cum_req.
   else
      l_plan_cumulative = 0.

   for each work_plan_schd exclusive-lock
         by work_plan_date
         by work_plan_time
         by work_plan_ref:

      /* WHEN THE FIRST WEEK OF THE SHIP SCHEDULE IS REACHED  */
      /* THEN SKIP THE REMAINING SCHEDULE LINES OF THE PLAN.  */
      if in_netting_logic = "3" and
         in_first_ship_week_date <> ? and
         work_plan_date <= in_first_ship_week_date then
         next.

      l_plan_cumulative = l_plan_cumulative + work_plan_qty.

      /* NOTE: ------------------------------------------ */
      /* WHEN USING SHIP/DLVY PATTERNS, REQUIREMENTS HAVE */
      /* BEEN BROKEN INTO DISCRETE QTYS & DATES EARLIER   */
      /* IN THIS PROGRAM.  THIS MAKES "REPLACE" LOGIC     */
      /* EASY, ALL DATES & QTYS UP TO AND INCLUDING THE   */
      /* CURRENT DATE ARE DISCARDED.                      */
      /* WHEN SHIP/DLVY PATTERNS = "", THEN WEEKLY        */
      /* AND MONTHLY QUANTITIES MUST BE ADJUSTED TO SHOW  */
      /* THE RESIDUAL QUANTITY REMAINING FOR THE INTERVAL.*/
      /* ADJUSTMENT LINES ARE WRITTEN TO THE REPORT.      */
      /* NOTE: ------------------------------------------ */

      if in_plan_sd_pat <> "" then do:
         if (work_plan_date <= in_last_seq_date and
            in_last_seq_date <> ?)  then do:

            /* DELETE IF TODAY OR EARLIER.      */
            delete work_plan_schd.
            next.
         end.
      end.
      else do:   /* else of in_plan_sd_pat <> = "" */

         /* WHEN SHIP/DLVY PATTERN IS "" THEN      */
         /* REQUIREMENTS NEED TO BE CHECKED FOR    */
         /* WEEKLY OR MONTHLY INTERVALS AND EITHER */
         /* DELETED FROM THE PLAN, OR ADJUSTED.    */

         /* DETERMINE INTERVAL */
         if in_plan_sd_pat = "" then
            for first schd_det where
            recid(schd_det) = work_plan_recid
            no-lock:
            end.

         if available (schd_det) then
         /* DISCARD OR ADJUST PLAN SCHEDULE BASED ON DATES */
         if (work_plan_week_date <= in_last_seq_week_date and
            work_plan_date <= in_last_seq_date and
            in_last_seq_date <> ?)  then do:

            /* INTERVAL PROCESSING:                            */
            /* DISCRETE - DELETE IF = LAST SEQ DAY OR EARLIER. */
            /* WEEKLY  - DELETE IF EARLIER THAN LAST SEQ WEEK. */
            /* MONTHLY - SAME AS WEEKLY.                       */
            /* *** MONTHLY INVERVALS HAVE BEEN BROKEN INTO *** */
            /* *** WEEKLY BUCKETS BUT STILL HAVE INTERVAL=M ** */

            if (work_plan_date <= in_last_seq_date and
               (schd_interval = "d" or
               schd_interval = " "))
               or
               (work_plan_week_date <
               in_last_seq_week_date  and
               (schd_interval = "w" or
               schd_interval = "m"))
            then do:
               delete work_plan_schd.
               next.
            end.
            else do:

               /* OTHERWISE .... MAKE MID-WEEK ADJUSTMENTS.   */
               /* WEEKLY  - ADJUST QTY IF IN LAST SEQ WEEK.   */
               /* MONTHLY - SAME AS WEEKLY.                   */
               /* STEPS:                                      */
               /* 1. IF SEQUENCES FOUND FOR WHOLE WEEK THEN   */
               /*    DELETE PLAN LINE.                        */
               /* 2. IF SEQUENCES NOT FOR WHOLE WEEK THEN     */
               /*    -- CALCULATE RESIDUAL                    */
               /*    -- PLACE RESIDUAL ON NEXT OPEN DATE      */
               /* 3. GET OUT.                                 */

               if (work_plan_week_date =
                  in_last_seq_week_date  and
                  (schd_interval = "w" or
                  schd_interval = "m")) then

                  /* DELETE PLAN IF SEQ COVERS WEEK */
                  if in_seq_end_of_week then do:
                     delete work_plan_schd.
                     next.
                  end.

               /* CALCULATE ADUSTMENT */
               assign
                  new_work_qty = work_plan_qty  - in_seq_weekly_cum
                  adjust_qty   = new_work_qty - work_plan_qty.

               /* DELETE PLAN IF NEW QTY <= 0 */
               if new_work_qty <= 0 then do:
                  delete work_plan_schd.
                  next.
               end.

               if report_yn and adjust_qty <> 0 then do:
                  display
                     work_plan_date
                     work_plan_time
                     work_plan_reference
                     work_plan_qty
                     adjust_qty
                     in_next_open_date @ new_work_date
                     new_work_qty
                  with frame seq_plan_ovlap STREAM-IO /*GUI*/ .

                  down 1 with frame seq_plan_ovlap.
               end.

               /* SET "NEW" QTY & DATE, THEN LEAVE */
               assign
                  work_plan_date = in_next_open_date
                  work_plan_qty  = new_work_qty.

               /* LEAVE WHEN USE-SHIP/PLAN-PCR = NO */
               if (in_netting_logic = "2"
                  or
                  (in_netting_logic = "3" and
                  in_use_ship_plan_pcr = no)) then
                  leave.

            end.  /* else of work_plan_date <= ... */

         end.  /* if (work_plan_week_date <= ... */

      end.  /* else of in_plan_sd_pat <> "" */

      /* SAVE THE CUMULATIVE QUANTITY TO-DATE OF THE      */
      /* PLAN SCHEDULE.  THIS IS WHERE THE PLAN SCHEDULE  */
      /* BEGINS. THE CUM QTY INCLUDES THE CUM QTY OF THE  */
      /* OF THE SEQUENCE SCHEDULE.  IT IS USED TO AS      */
      /* PRIOR CUM IN THE "REPLACE" LOGIC.                */

      if l_first_plan_record then
      assign
         out_first_plan_cumulative = l_plan_cumulative - work_plan_qty
         l_first_plan_record = no.

   end. /*  for each work_plan_schd */

END PROCEDURE.  /* replace_plan_with_seq_schedule */

PROCEDURE replace_plan_with_ship_schedule:
   define input parameter in_plan_cumulative
      like schd_cum_qty no-undo.
   define input parameter in_ship_cumulative
      like schd_cum_qty no-undo.
   define input parameter in_plan_sd_pat like sch_sd_pat no-undo.
   define input parameter in_use_ship_plan_pcr
      like mfc_logical no-undo.
   define input parameter in_last_seq_date
      like schd_date no-undo.
   define input parameter in_last_seq_week_date
      like schd_date no-undo.
   define input parameter in_seq_cumulative
      like schd_cum_qty no-undo.
   define input parameter in_seq_weekly_cum
      like schd_cum_qty no-undo.
   define input parameter in_seq_end_of_week
      like mfc_logical no-undo.
   define input parameter in_next_open_date like schd_date no-undo.

   define variable l_last_ship_date like schd_date no-undo.
   define variable l_last_ship_week_date like schd_date no-undo.
   define variable l_ship_weekly_cum like schd_cum_qty no-undo.
   define variable l_ship_end_of_week like mfc_logical no-undo.
   define variable l_next_open_date like schd_date no-undo.
   define variable l_plan_cumulative like schd_cum_qty no-undo.
   define variable l_ship_cumulative like schd_cum_qty no-undo.

   assign
      l_plan_cumulative = in_plan_cumulative
      l_ship_cumulative = in_ship_cumulative.

   /* SCAN THE SHIP WORKFILE TO BUILD A LIST OF PARAMETERS */

   run process_ship_schedule_data
      (input-output l_ship_cumulative,
       output l_last_ship_date,
       output l_last_ship_week_date,
       output l_ship_weekly_cum,
       output l_ship_end_of_week,
       output l_next_open_date).

   /* IF NO SHIP SCHEDULE THEN USE SEQ SCHEDULE PARAMETERS */

   if l_last_ship_date = ? then
   assign
      l_ship_cumulative       = in_seq_cumulative
      l_last_ship_date        = in_last_seq_date
      l_last_ship_week_date   = in_last_seq_week_date
      l_ship_weekly_cum       = in_seq_weekly_cum
      l_ship_end_of_week      = in_seq_end_of_week
      l_next_open_date        = in_next_open_date.

   for each work_plan_schd exclusive-lock
         by work_plan_date
         by work_plan_time
         by work_plan_ref:

      l_plan_cumulative = l_plan_cumulative + work_plan_qty.

      /* NOTE: ------------------------------------------ */
      /* WHEN USING SHIP/DLVY PATTERNS, REQUIREMENTS HAVE */
      /* BEEN BROKEN INTO DISCRETE QTYS & DATES EARLIER   */
      /* IN THIS PROGRAM.  THIS MAKES "REPLACE" LOGIC     */
      /* EASY, ALL DATES & QTYS UP TO AND INCLUDING THE   */
      /* CURRENT DATE ARE DISCARDED.                      */
      /* WHEN SHIP/DLVY PATTERNS = "", THEN WEEKLY        */
      /* AND MONTHLY QUANTITIES MUST BE ADJUSTED TO SHOW  */
      /* THE RESIDUAL QUANTITY REMAINING FOR THE INTERVAL.*/
      /* ADJUSTMENT LINES ARE WRITTEN TO THE REPORT.      */
      /* NOTE: ------------------------------------------ */

      /* NOTE: ------------------------------------------ */
      /* THIS CODE CORRECTS A BUG IN THAT OCCURS WHEN     */
      /* THE SHIP SCHEDULE ENDS MID-WEEK AND THE PLANNING */
      /* SCHEDULE HAS A WEEKLY REQUIREMENT AND AN SDP     */
      /* VALUE OF "".  THE OLD CODE WOULD DROP A WEEKLY   */
      /* REQUIREMENT BECAUSE IT PROCESSED BASED ON DATES  */
      /* AND THE WEEKLY DATE (MONDAY) IS EARLIER THAN THE */
      /* MID-WEEK DATE.  THUS THE TOTAL WEEKLY REQUIREMENT*/
      /* IS UNDERSTATED.  THIS NEW LOGIC CALCULATES THE   */
      /* RESIDUAL FOR THE WEEK AND ADJUSTS THE PLANNING   */
      /* SCHEDULE.                                        */
      /* NOTE: ------------------------------------------ */

      if in_plan_sd_pat <> "" then do:
         if (work_plan_date <= l_last_ship_date and
            l_last_ship_date <> ?)  then do:

            /* DELETE IF TODAY OR EARLIER.      */
            delete work_plan_schd.
            next.
         end.
      end.
      else do:   /* else of in_plan_sd_pat <> = "" */

         /* WHEN SHIP/DLVY PATTERN IS "" THEN      */
         /* REQUIREMENTS NEED TO BE CHECKED FOR    */
         /* WEEKLY OR MONTHLY INTERVALS AND EITHER */
         /* DELETED FROM THE PLAN, OR ADJUSTED.    */

         /* DETERMINE INTERVAL */
         if in_plan_sd_pat = "" then
            for first schd_det where
               recid(schd_det) = work_plan_recid
               no-lock:
            end.

         if available (schd_det) then
            /* DISCARD OR ADJUST PLAN SCHEDULE BASED ON DATES */
            if (work_plan_week_date <= l_last_ship_week_date and
                work_plan_date <= l_last_ship_date and
               l_last_ship_date <> ?)
            then do:

               /* INTERVAL PROCESSING:                            */
               /* DISCRETE - DELETE IF = LAST SEQ DAY OR EARLIER. */
               /* WEEKLY  - DELETE IF EARLIER THAN LAST SEQ WEEK. */
               /* MONTHLY - SAME AS WEEKLY.                       */
               /* *** MONTHLY INVERVALS HAVE BEEN BROKEN INTO *** */
               /* *** WEEKLY BUCKETS BUT STILL HAVE INTERVAL=M ** */

               if (work_plan_date <= l_last_ship_date and
                  (schd_interval = "d" or
                  schd_interval = " "))
                  or
                  (work_plan_week_date <
                  l_last_ship_week_date  and
                  (schd_interval = "w" or
                  schd_interval = "m"))
               then do:
                  delete work_plan_schd.
                  next.
               end.
               else do:

                  /* OTHERWISE.... MAKE MID-WEEK ADJUSTMENTS.    */
                  /* WEEKLY  - ADJUST QTY IF IN LAST SEQ WEEK.   */
                  /* MONTHLY - SAME AS WEEKLY.                   */
                  /* STEPS:                                      */
                  /* 1. IF SHIP LINE FOUND FOR WHOLE WEEK THEN   */
                  /*    DELETE PLAN LINE.                        */
                  /* 2. IF SEQUENCES NOT FOR WHOLE WEEK THEN     */
                  /*    -- CALCULATE RESIDUAL                    */
                  /*    -- PLACE RESIDUAL ON NEXT OPEN DATE      */
                  /* 3. GET OUT.                                 */

                  if (work_plan_week_date =
                      l_last_ship_week_date  and
                     (schd_interval = "w" or
                      schd_interval = "m")) then

                     /* IF SHIP COVERS WEEK, THEN DELETE PLAN */
                     if l_ship_end_of_week
                        or
                        (ship_interval = "w" or
                        ship_interval = "m") then do:
                        delete work_plan_schd.
                        next.
                     end.

                  /* CALCULATE ADJUSTMENT */
                  assign
                     new_work_qty = work_plan_qty - l_ship_weekly_cum
                     adjust_qty   = new_work_qty - work_plan_qty.

                  /* IF NEW PLAN QTY <= 0 THEN DELETE PLAN */
                  if new_work_qty <= 0 then do:
                     delete work_plan_schd.
                     next.
                  end.

                  if report_yn and adjust_qty <> 0 then do:
                     display
                        work_plan_date
                        work_plan_time
                        work_plan_reference
                        work_plan_qty
                        adjust_qty
                        l_next_open_date @ new_work_date
                        new_work_qty
                     with frame planovlap STREAM-IO /*GUI*/ .

                     down 1 with frame planovlap.
                  end.

                  /* SET "NEW" QTY & DATE */
                  assign
                     work_plan_date = l_next_open_date
                     work_plan_qty  = new_work_qty.

                  /* LEAVE WHEN USE-SHIP/PLAN-PCR = NO */
                  if not in_use_ship_plan_pcr then
                    leave.

               end.  /* else of work_plan_date <= ... */
            end.  /* if (work_plan_week_date <= ... */
      end.  /* else of in_plan_sd_pat <> "" */

      /* "USE SHIP/PLAN PCR" (7.9.24) OPTION. */
      /* ADJUST QTY OF THE PLAN SCHEDULE TO   */
      /* MATCH CUM OF THE SHIP SCHEDULE.      */
      /* EXTRA PLAN LINES ARE DELETED; AND    */
      /* RESIDUAL IS ADDED TO THE "NEXT" LINE.*/
      /* THEN, GET OUT!                       */

      if in_use_ship_plan_pcr then do:
         if ((l_plan_cumulative <= l_ship_cumulative) or
            (work_plan_date <= l_last_ship_date and
            l_last_ship_date <> ? )) then do:
            delete work_plan_schd.
            next.
         end. /* end of if */

         /* CALCULATE ADJUSTMENT */
         assign
            new_work_qty = l_plan_cumulative - l_ship_cumulative
            adjust_qty = new_work_qty - work_plan_qty.

         if report_yn and adjust_qty <> 0 then do:
            display
               work_plan_date
               work_plan_time
               work_plan_reference
               work_plan_qty
               adjust_qty
               work_plan_date @ new_work_date
               new_work_qty
            with frame planovlap STREAM-IO /*GUI*/ .

            down 1 with frame planovlap.
         end.

         work_plan_qty = new_work_qty.
         leave.
      end.  /* if mfc_logical */

   end.  /* for each work_plan_schd */
END PROCEDURE.  /* replace_plan_with_ship_schedule */

PROCEDURE replace_ship_with_seq_schedule:
   define input parameter in_ship_sd_pat like sch_sd_pat no-undo.
   define input parameter in_use_ship_plan_pcr
      like mfc_logical no-undo.
   define input parameter in_netting_logic like sod_conrep no-undo.
   define input parameter in_ship_prior_cum_req
      like schd_cum_qty no-undo.
   define input parameter in_last_seq_date like schd_date no-undo.
   define input parameter in_last_seq_week_date
      like schd_date no-undo.
   define input parameter in_seq_cumulative
      like schd_cum_qty no-undo.
   define input parameter in_seq_weekly_cum
      like schd_cum_qty no-undo.
   define input parameter in_seq_end_of_week
      like mfc_logical no-undo.
   define input parameter in_next_open_date
      like schd_date no-undo.

   define output parameter out_first_ship_cumulative
      like schd_cum_qty no-undo.
   define output parameter out_first_ship_week_date
      like schd_date no-undo.

   define variable l_ship_cumulative like schd_cum_qty no-undo.
   define variable l_first_ship_record like mfc_logical no-undo
      initial yes.

   if ship_prior_cum_req <> ? then
      l_ship_cumulative = in_ship_prior_cum_req.
   else
      l_ship_cumulative = 0.

   for each work_ship_schd exclusive-lock
         break by work_ship_week_date
         by work_ship_date
         by work_ship_time
         by work_ship_ref:

      l_ship_cumulative = l_ship_cumulative + work_ship_qty.

      /* NOTE: ------------------------------------------ */
      /* WHEN USING SHIP/DLVY PATTERNS, REQUIREMENTS HAVE */
      /* BEEN BROKEN INTO DISCRETE QTYS & DATES EARLIER   */
      /* IN THIS PROGRAM.  THIS MAKES "REPLACE" LOGIC     */
      /* EASY, ALL DATES & QTYS UP TO AND INCLUDING THE   */
      /* CURRENT DATE ARE DISCARDED.                      */
      /* WHEN SHIP/DLVY PATTERNS = "", THEN WEEKLY        */
      /* AND MONTHLY QUANTITIES MUST BE ADJUSTED TO SHOW  */
      /* THE RESIDUAL QUANTITY REMAINING FOR THE INTERVAL.*/
      /* ADJUSTMENT LINES ARE WRITTEN TO THE REPORT.      */
      /* NOTE: ------------------------------------------ */

      if in_ship_sd_pat <> "" then do:
         if (work_ship_date <= in_last_seq_date and
            in_last_seq_date <> ?) then do:

            /* DELETE IF TODAY OR EARLIER.  */
            delete work_ship_schd.
            next.

         end.
      end.
      else do:    /* else of in_ship_sd_pat <> = "" */

         /* WHEN SHIP/DLVY PATTERN IS "" THEN      */
         /* REQUIREMENTS NEED TO BE CHECKED FOR    */
         /* WEEKLY OR MONTHLY INTERVALS AND EITHER */
         /* DELETED FROM THE SHIP, OR ADJUSTED.    */

         /* DETERMINE INTERVAL */
         for first schd_det
               where recid(schd_det) = work_ship_recid
               no-lock:
         end.

         if available (schd_det) then
            /* DISCARD OR ADJUST SHIP SCHED BASED ON DATES */
            if (work_ship_week_date <= in_last_seq_week_date and
                work_ship_date <= in_last_seq_date and
                in_last_seq_date <> ?) then do:

               /* INTERVAL PROCESSING:                            */
               /* DISCRETE - DELETE IF = LAST SEQ DAY OR EARLIER. */
               /* WEEKLY  - DELETE IF EARLIER THAN LAST SEQ WEEK. */
               /* MONTHLY - SAME AS WEEKLY.                       */
               /* *** MONTHLY INVERVALS HAVE BEEN BROKEN INTO *** */
               /* *** WEEKLY BUCKETS BUT STILL HAVE INTERVAL=M ** */

               if (work_ship_date <= in_last_seq_date and
                  (schd_interval = "d" or
                  schd_interval = " "))
                  or
                  (work_ship_week_date <
                  in_last_seq_week_date and
                  (schd_interval = "w" or
                  schd_interval = "m"))
               then do:
                  delete work_ship_schd.
                  next.
               end.
               else do:

                  /* OTHERWISE .... MAKE MID-WEEK ADJUSTMENTS.   */
                  /* WEEKLY  - ADJUST QTY IF IN LAST SEQ WEEK.   */
                  /* MONTHLY - SAME AS WEEKLY.                   */
                  /* STEPS:                                      */
                  /* 1. IF SEQUENCES FOUND FOR WHOLE WEEK THEN   */
                  /*    DELETE SHIP LINE.                        */
                  /* 2. IF SEQUENCES NOT FOR WHOLE WEEK THEN     */
                  /*    -- CALCULATE RESIDUAL                    */
                  /*    -- PLACE RESIDUAL ON NEXT OPEN DATE      */
                  /* 3. GET OUT.                                 */

                  if (work_ship_week_date =
                      in_last_seq_week_date  and
                     (schd_interval = "w" or
                     schd_interval = "m")) then

                     /* DELETE SHIP IF SEQ COVERS WEEK */
                     if in_seq_end_of_week then do:
                        delete work_ship_schd.
                        next.
                     end.

                  /* CALCULATE ADJUSTMENT */
                  assign
                     new_work_qty = work_ship_qty - in_seq_weekly_cum
                     adjust_qty = new_work_qty - work_ship_qty.

                  /* DELETE SHIP IF NEW SHIP QTY <= 0 */
                  if new_work_qty <= 0 then do:
                     delete work_ship_schd.
                     next.
                  end.

                  if report_yn and adjust_qty <> 0 then do:
                     display
                        work_ship_date
                        work_ship_time
                        work_ship_reference
                        work_ship_qty
                        adjust_qty
                        in_next_open_date
                        @ new_work_date
                        new_work_qty
                     with frame seq_ship_ovlap STREAM-IO /*GUI*/ .

                     down 1 with frame seq_ship_ovlap.
                  end.

                  /* SET "NEW" QTY & DATE */
                  assign
                     work_ship_date = in_next_open_date
                     work_ship_qty  = new_work_qty.

                  /* LEAVE WHEN USE-SHIP/PLAN-PCR = NO */
                  if (in_netting_logic = "1" or
                      in_netting_logic = "4"
                     or
                     (in_netting_logic = "3" and
                      in_use_ship_plan_pcr = no)) then
                     leave.

               end.  /* else of work_ship_date <= ... */

            end.  /* if (work_ship_week_date <= ... */

      end.  /* else of in_ship_sd_pat <> = "" */

      /* SAVE THE "NEW" WEEK DATE AND CUM QTY OF THE      */
      /* SHIP SCHEDULE.  THIS IS WERE THE SHIP SCHEDULE   */
      /* BEGINS. THE CUM QTY TAKES INCLUDES THE CUM QTY   */
      /* OF THE SEQUENCE SCHEDULE.  IT IS USED TO AS      */
      /* PRIOR CUM IN THE "CONSUME" LOGIC.                */
      if l_first_ship_record then
      assign
         out_first_ship_week_date  = work_ship_week_date
         out_first_ship_cumulative = l_ship_cumulative - work_ship_qty
         l_first_ship_record = no.

      /* "USE SHIP/PLAN PCR" (7.9.24) OPTION. */
      /* ADJUST QTY OF THE SHIP SCHEDULE TO   */
      /* MATCH CUM OF THE SEQ. SCHEDULE.      */
      /* EXTRA SHIP LINES ARE DELETED; AND    */
      /* RESIDUAL IS ADDED TO THE "NEXT" LINE.*/
      /* THEN, GET OUT!                       */

      if in_netting_logic = "3" and in_use_ship_plan_pcr then do:

         if l_ship_cumulative <= in_seq_cumulative then
            /* DELETE SHIP LINE */
            delete work_ship_schd.
         else do:

            /* CALCULATE ADJUSTMENT */
            assign
               new_work_qty = l_ship_cumulative - in_seq_cumulative
               adjust_qty = new_work_qty - work_ship_qty.

            if report_yn and adjust_qty <> 0 then do:
               display
                  work_ship_date
                  work_ship_time
                  work_ship_reference
                  work_ship_qty
                  adjust_qty
                  in_next_open_date
                  @ new_work_date
                  new_work_qty
               with frame seq_ship_ovlap STREAM-IO /*GUI*/ .

               down 1 with frame seq_ship_ovlap.
            end.  /* if report_yn and adjust_qty <> 0 */

            /* ADJUST SHIP LINE */
            assign
               work_ship_qty =  new_work_qty
               out_first_ship_cumulative = l_ship_cumulative -
                                           work_ship_qty.
            leave.
         end.  /* else of do */

      end.  /* if in_netting_logic = "3" */

   end. /*  for each work_ship_schd */

END PROCEDURE.  /* replace_ship_with_seq_schedule */

PROCEDURE delete_work_schd_tables:
   for each work_schd exclusive-lock:
      delete work_schd.
   end.
   for each work_ship_schd exclusive-lock:
      delete work_ship_schd.
   end.
   for each work_plan_schd exclusive-lock:
      delete work_plan_schd.
   end.
   for each work_seq_schd exclusive-lock:
      delete work_seq_schd.
   end.
END PROCEDURE.

PROCEDURE get_ship_plan_pcr:
   define output parameter ship_plan_pcr as logical no-undo.
   for first mfc_ctrl
         fields (mfc_field mfc_logical)
         where mfc_field = "rcc_ship_plan_pcr" no-lock:
   end. /* FOR FIRST MFC_CTRL */

   if not available mfc_ctrl then do:
      create mfc_ctrl.

      assign
         mfc_label   = getTermLabel("USE_SHIP/PLAN_PCR",24)
         mfc_field   = "rcc_ship_plan_pcr"
         mfc_seq     = 16
         mfc_module  = "rc"
         mfc_type    = "l"
         mfc_logical = no.
   end. /* IF NOT AVAILABLE */
   ship_plan_pcr = mfc_logical.
END PROCEDURE.
